Django ORM过滤器给出了一个QuerySet对象,我无法从中检索数据


-----------------------------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)

所以这是我尝试访问领子属性的模板,希望这能帮助您更好地了解我的问题......

-----------------------------模板---------------------------- {% 扩展 '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 %}

在模板中,我无法通过{{ collar.inner.url }}或检索图像网址{{ collar.upper.url }}等我不确定出了什么问题。当我尝试在视图中打印领子时,print(collar)它给出了<QuerySet [<Collar: Collar object (1)>]>. 我不确定如何处理QuerySet对象以获取所需的数据。任何帮助将不胜感激...

collar = pattern.collar.all().filter(catagory="RR")返回查询集而不是记录。要打印collar,您应该将其放在模板中的 for 循环中,例如

{% for collr in collar %}
{{ collr.inner.url }} 
{%endfor%}

或者只是使用collar = pattern.collar.filter(catagory="RR").first()将项圈作为单个记录传递到模板

相关内容

  • 没有找到相关文章

最新更新