I’m stuck at the Write views that actually do something section.
I’ve modified my views to be the following as instructed:
from django.template import Context, loader
from polls.models import Poll
from django.http import HttpResponse
def index(request):
latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
t = loader.get_template('polls/index.html')
c = Context({
'latest_poll_list': latest_poll_list,
})
return HttpResponse(t.render(c))
def detail(request, poll_id):
return HttpResponse("You're looking at poll %s." % poll_id)
def results(request, poll_id):
return HttpResponse("You're looking at the results of the poll %s." % poll_id)
def vote(request, poll_id):
return HttpResponse("You're voting on poll %s." % poll_id)
I’ve made my templates directory to be /home/stanley/mytemplates/polls/ as instructed in the tutorial, and this is the relevant line which matches in the settings.py:
TEMPLATE_DIRS = (
"/home/stanley/mytemplates/",
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
However, I’m still seeing the following error message in my browser, after running the server in the localhost (http://127.0.0.1:8000/polls/index.html):
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/polls/index.html
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/$
^polls/(?P<poll_id>\d+)/$
^polls/(?P<poll_id>\d+)/results/$
^polls/(?P<poll_id>\d+)/vote/$
^admin/
^admin/
The current URL, polls/index.html, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
I’m doing something wrong with my code or files, but can’t quite figure out exactly what.
The url for the index view is
/polls/, not/polls/index.html.If you want /polls/index.html to work, you have to add a url pattern for it, for example:
However, you probably don’t want to do that. One of the nice things about Django is that you can define the urls independently of the views and templates, so you don’t need ‘crufty’ urls that end in
.html.