Python IndexView缺少查询集。定义IndexView.model-教程4 django
我跟在后面。在页面末尾,我必须修改views.py和url.py URL.py:Python IndexView缺少查询集。定义IndexView.model-教程4 django,python,django,Python,Django,我跟在后面。在页面末尾,我必须修改views.py和url.py URL.py: from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.IndexView.as_view(), name='index'), url(r'^(?P<pk>[0-9]+)/$', views
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$',
views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[0-9]+)/results/$',
views.ResultsView.as_view(), name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$',
views.vote, name='vote'),
]
当我尝试访问管理网站或投票应用程序时,我得到了以下信息:
在/轮询时配置不当/
IndexView
应为
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
"""Return the last five published questions."""
return Question.objects.order_by('-pub_date')[:5]
缩进很重要。我猜您的索引视图是
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
"""Return the last five published questions."""
return Question.objects.order_by('-pub_date')[:5]
//您必须在index.html{{latest.question.list}中使用相同的context\u object\u name这可能是一件愚蠢的事情,但您的缩进正确吗?导入和views.py中的以下行
您是否阅读了前三个教程,如果是这样,我认为您不应该出现此错误当然数据库已设置并填充,对吗?在admin.py
中,您注册了模型吗<代码>管理站点.注册(模型.问题)
是!非常感谢。我得把表列两次!我以前也用制表符来表示身份,但最终我意识到用4个空格更好。随着时间的推移,你会发现使用4个空间会更好(即使一开始感觉不太舒服)。PEP-0008建议这样做。此外,在其他编辑器中默认情况下tabstop=8并必须对其进行编辑,或者在GitHub中,这也是一件令人恼火的事情。有关更多信息,请参阅。
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
"""Return the last five published questions."""
return Question.objects.order_by('-pub_date')[:5]
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
"""Return the last five published questions."""
return Question.objects.order_by('-pub_date')[:5]