看法:
def details(request, id):
event = Event.objects.filter(id=id)
return render(request, 'details.html', {'Event' : event})
模板:
{% if Event %}
<h1>Event Name: {{ Event.name }}</h1>
{% else %}
No Event.
{% endif %}
模型:
class Event(models.Model):
name = models.CharField(max_length = 30)
artist = models.ManyToManyField(Artist)
venue = models.ForeignKey(Venue)
event_date = models.DateTimeField('event date')
on_sale = models.DateTimeField('on sale time')
def __unicode__(self):
return unicode(self.name)
当我加载它只是加载页面Event Name:
。当我删除.name
属性时,它会加载事件,因为它是在数据库中输入的,但它不会从对象加载任何属性。感谢任何帮助/建议,谢谢。