2016-07-10 23 views
0

我正在關注網站上的django教程,並且我堅持使用通用視圖。Django:通用視圖不能正常工作

views.py

class DetailView(generic.DetailView): 
    model = Questions 
    template_name = 'tolls/detail.html' 

urls.py

url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail') 

detail.html

<h1> {{question.question_text}} </h1> 

沒有顯示在我的details.html

如果我不」使用通用視圖,它工作機智以下功能和url

view.py

def detial(request, question_id): question = get_object_or_404(Questions, pk=question_id) return render(request, 'tolls/detail.html', {'question': question})

urls.py

`url(r'^(?P<question_id>[0-9]+)/$', views.detial, name='detail'),` 

回答

1

h對於訪問您模型實例的默認名稱爲object。因此,在模板中使用{{ object.question_text }}或在您的視圖類中使用context_object_name指定名稱:

class DetailView(generic.DetailView): 
    model = Questions 
    template_name = 'tolls/detail.html' 
    context_object_name = 'question'