我正在做 Django 教程,第 3 部分:https ://docs.djangoproject.com/en/dev/intro/tutorial03/
我目前在我的views.py中有这个文本:
from django.http import HttpResponse
from polls.models import Poll
def index(request):
latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
output = ', '.join([p.question for p in latest_poll_list])
return HttpResponse(output)
这在我去时有效:
http://localhost:8000/polls/
,它显示记录
问题是当我采取下一步并将views.py更改为:
from django.shortcuts import render
from polls.models import Poll
def index(request):
latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
context = {'latest_poll_list': latest_poll_list}
return render(request, 'polls/index.html', context)
从http://localhost:8000/polls/
它显示以下错误:TemplateDoesNotExist at /polls/ 从http://localhost:8000/polls/index.html
它显示此错误:找不到页面(404)
我将 mysite/urls.py 设置为:
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
我将 poll/views.py 设置为:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
我将 TEMPLATE_DIRS 设置为:
'/path/to/mysite/templates'
为什么页面不会加载新代码?