开发者

Django template object return empty value

开发者 https://www.devze.com 2023-03-04 04:32 出处:网络
I filter a subject from database: subject = Subject.objects.filter(id=1) I tried to call it form template:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消