我现在已经在这里玩了很长时间了。我有一个问题,模板没有渲染我的活动场地。我只得到{%else%}声明。任何帮助是极大的赞赏。谢谢!
模型:
class Shows(models.Model):
event_date = models.DateField("Date of event")
venue = models.CharField(max_length=100)
def __unicode__(self):
return u'%s' % (self.venue)
的观点:
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from events.models import Shows
def show_results(request):
"""
Events - this is the events section.
"""
events = Shows.objects.all()
return render_to_response("events/events.html", {
"events": events,
}, context_instance=RequestContext(request))
模板:
<h2>These are events for this month</h2>
{% if events %}
{% for e in events %}
<ul>
<li>{{ e.venue }}</li>
</ul>
{% endfor %}
{% else %}
<p>There are no events at this time.</p>
{% endif %}
文件结构:
/myapp
/deploy
/events
/templates
/events
events.html
admin.py
models.py
urls.py
views.py
/requirements
/static
/templates
manage.py
settings.py
urls.py
答案 0 :(得分:0)
您的模板正在尝试迭代events
,就像它是Shows
个实例的列表一样。但是,get_object_or_404
返回单个对象。现在,您可能只在DB中有一条记录,因此get_object_or_404
不会抛出异常。
为了让events
存储对象列表而不是单个实例,您可以将get_object_or_404
替换为:
events = Shows.objects.all()
这将返回所有Shows
个实例的列表。