Django 1.7 tutorial part 4, url

97 views
Skip to first unread message

Daniel Altschuler

unread,
Mar 4, 2015, 2:15:32 PM3/4/15
to django...@googlegroups.com
I ran into the following problem with the tutorial.

When I'm at http://127.0.0.1:8000/polls/1/, I get the expected page:


What's up?

 
 


However when I try to vote I get the error 

Page not found (404)

Request Method:POST
Request URL:http://127.0.0.1:8000/polls/1/vote/

No Question matches the given query.

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.


After some time I found that something is wrong with the urls. My polls/urls.py file was:

-----------------
from django.conf.urls import patterns, url

from polls import views

urlpatterns = patterns('',
    # ex: /polls/
    url(r'^$', views.index, name='index'),
    # ex: /polls/5/
    url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),

    url(r'^(?P<begins_with>\S+)/$', views.search, name='search'),
    # ex: /polls/5/results/
    url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'),
    # ex: /polls/5/vote/
    url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
)
------------------

If I remove the "/" after "vote", then everything works. I had the same error when attempting to view the results, so I also
removed the "/" after results. My urls.py now looks like

    ...
    # ex: /polls/5/results
    url(r'^(?P<question_id>\d+)/results$', views.results, name='results'),
    # ex: /polls/5/vote
    url(r'^(?P<question_id>\d+)/vote$', views.vote, name='vote'),
   ...

Can someone explain me what is going on? thanks.

Note that the file mysite/urls.py is:

----------
from django.conf.urls import patterns, url

from polls import views

urlpatterns = patterns('',
    # ex: /polls/
    url(r'^$', views.index, name='index'),
    # ex: /polls/5/
    url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),

    url(r'^(?P<begins_with>\S+)/$', views.search, name='search'),
    # ex: /polls/5/results
    url(r'^(?P<question_id>\d+)/results$', views.results, name='results'),
    # ex: /polls/5/vote
    url(r'^(?P<question_id>\d+)/vote$', views.vote, name='vote'),
)
--------

Collin Anderson

unread,
Mar 6, 2015, 7:39:36 PM3/6/15
to django...@googlegroups.com
Hi,

That seems strange. The error says that there's no question with id=1. Are you sure there's a question with id=1?

The ending slash seems important to me. I don't know why it would work without it.

Collin

luis zarate

unread,
Mar 7, 2015, 11:32:40 AM3/7/15
to django...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/db705c9f-f8a1-46e6-add8-a637671a603e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
"La utopía sirve para caminar" Fernando Birri

luis zarate

unread,
Mar 7, 2015, 11:33:02 AM3/7/15
to django...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages