2013-06-21 59 views
0

查看:無法訪問屬性在Django的模板

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屬性時,它會在事件輸入數據庫時​​加載該事件,但不會加載該對象的任何屬性。感謝任何幫助/建議,謝謝。

回答

5

您應該使用GET而不是過濾器:

event = Event.objects.get(id=id) 

如果你要使用過濾器在templete試試這個:

{% for item in Event %} 
    <h1>Event Name: {{ item.name }}</h1> 
{% empty %} 
    No Event. 
{% endfor %}