I filter a subject from database:
subject = Subject.objects.filter(id=1)
I tried to call it form template:
{{ subject.name|safe }}
It return empty value. Do I need to loop开发者_Go百科 the object? It just return a single records:
>>> subject[0].name
u'010-01 INTERNATIONAL : Organizations'
But when I put {{ subject[0].name|safe }}, it return no value too.
Try:
{{subject.0.name}}
If you're only getting one object from the database, you should typically use
try:
subject = Subject.objects.get(id=1)
except Subject.DoesNotExist:
pass #do whatever handling stuff you need to do here
You could also use the get_object_or_404 shortcut.
精彩评论