引用从view.py传递的模板中的实例属性 - django 2.0

时间:2018-06-14 09:25:52

标签: django django-templates

希望是一个简单的......

views.py
def View(request):
    queryset = Example.objects.all()
    for obj in queryset :
        obj.x = 'String attribute added to model instance.'
    context = {'queryset ': queryset }
    return render(request, 'app/template.html', context=context)

template.html
{% for obj in queryset.all %}
    {{ obj.x }}
{% endfor %}

根据Stefano Contini的article,这应该可行,但在我的例子{{obj.x}}中并没有完成这个字符串。我做错了什么?

1 个答案:

答案 0 :(得分:0)

{%for queryj.all%}中的obj不正确

{%对于queryset%中的obj}是正确的

感谢@Daniel Roseman提出回答问题的问题