I’m trying to access the detail view, but it doesn’t seem to be mapping properly. Is there something I’m missing? When I try to access the localhost:8000/polls/1 view I get a 404 saying the the url pattern doesn’t match any of those url patterns given below. Is there something wrong with my regular expression?
Here are the url patterns in the 404:
^polls/ ^$
^polls/ ^polls/(?P<poll_id>\d+)/$
^polls/ ^polls/(?P<poll_id>\d+)/results/$
^polls/ ^polls/(?P<poll_id>\d+)/vote/$
^admin/
The current URL, polls/1/vote/, didn’t match any of these.
Here is urls.py:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('polls.views',
url (r'^$', 'index'),
url (r'^polls/(?P<poll_id>\d+)/$', 'detail'),
url (r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
url (r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
)
Here is my views.py:
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response, get_object_or_404
from polls.models import Choice,Poll
def index(request):
latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
return render_to_response('polls/index.html', {'latest_poll_list': latest_poll_list})
def detail(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
return render_to_response('polls/detail.html', {'poll': p},
context_instance=RequestContext(request))
def results(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
return render_to_response('polls/results.html', {'poll':p})
def vote(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
#Redisplay poll voting form
return render_to_response('polls/detail.html', {
'poll': p,
'error_message': "You didn't select a choice.",
}, context_instance=RequestContext(request))
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('polls.views.results', args=(p.id,)))
I guess you are including your app urls in project urls file which is why in the error urls you can see polls written twice. I would suggest you to remove ‘polls’ word from your app urls
and try again..