Reputation: 401
-----------------------------Models-----------------------------
class Pattern(models.Model):
name = models.CharField(_("Pattern Name"), unique=True, max_length=32)
created_on = models.DateTimeField(_("Created On"), editable=False, auto_now_add=True)
class Base(models.Model):
l_shoulder = models.ImageField(_("Left Shoulder"), upload_to='left_shoulders/')
r_shoulder = models.ImageField(_("Right Shoulder"), upload_to='right_shoulders/')
l_front = models.ImageField(_("Left Front"), upload_to='left_fronts/')
r_front = models.ImageField(_("Right Front"), upload_to='right_fronts/')
l_collar_base = models.ImageField(_("Left Collar Base"), upload_to='left_collor_bases/')
r_collar_base = models.ImageField(_("Right Collar Base"), upload_to='right_color_bases/')
yoke_bottom = models.ImageField(_("Bottom Yoke"), upload_to='neck_bottoms/')
yoke_top = models.ImageField(_("Top Yoke"), upload_to='neck_tops/')
placket = models.ImageField(_("Placket"), upload_to='plackets/')
pattern = models.OneToOneField(Pattern, on_delete=models.CASCADE)
class Collar(models.Model):
CATAGORY_CHOICES = (
('RR', 'Regular'),
('BR', 'Big Round'),
('CA', 'Cut Away'),
('DB', 'Dual Button'),
('PH', 'Pin Hole'),
('SW', 'Semi Wide'),
('RB', 'Round Button Down'),
('SP', 'Short Point'),
('SS', 'Stand'),
('WS', 'Wide Spread'),
)
inner = models.ImageField(_("Inner Collar"), upload_to="inner_collars/")
upper = models.ImageField(_("Upper Collar"), upload_to="upper_collars/")
outer_r = models.ImageField(_("Outer Right Collar"), upload_to="outer_right_collars/")
outer_l = models.ImageField(_("Outer Left Collar"), upload_to="outer_left_collars/")
catagory = models.CharField(max_length=2, choices=CATAGORY_CHOICES)
pattern = models.ForeignKey(Pattern, related_name="collar", on_delete=models.CASCADE)
-----------------------------Views-----------------------------
def design(request):
pattern = Pattern.objects.get(name="p1")
collar = pattern.collar.all().filter(catagory="RR")
cont = {
"base": pattern.base,
"collar": collar,
}
return render(request, 'shirts/shirtdesign.html', context=cont)
So here is the template in which i am trying to access the collar properties, Hope this will help you understand my problem in better way...
-----------------------------Templates---------------------------- {% extends 'base.html' %}
{% block title %}
Design
{% endblock title %}
{% block content %}
Design Your Shirt Here...
<div style="height:500px; width:400px;">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.l_shoulder.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.r_shoulder.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.l_front.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.r_front.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.l_collar_base.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.r_collar_base.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.yoke_top.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.yoke_bottom.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.placket.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ collar.inner.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ collar.upper.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ collar.outer_r.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ collar.outer_l.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ collar.button.url }}">
</div>
{% endblock content %}
In template, I am unable to retreive image urls by {{ collar.inner.url }}
or
{{ collar.upper.url }}
etc. I am not sure what's wrong. When i try to print collar in views as print(collar)
it gives <QuerySet [<Collar: Collar object (1)>]>
.
I am not sure what to do with QuerySet object to get the desired data. Any help would be much appreciated...
Upvotes: 0
Views: 25
Reputation: 1452
collar = pattern.collar.all().filter(catagory="RR")
returns a queryset not a record . To print the collar
you should put that in a forloop in the template like
{% for collr in collar %}
{{ collr.inner.url }}
{%endfor%}
or just pass the collar as single record to template using collar = pattern.collar.filter(catagory="RR").first()
Upvotes: 1