Daniel Daniel - 8 months ago 74
Python Question

URL mapping in Django (Tutorial)

I started with Django using the tutorial with the polls (as seen on the PyCharm site https://www.jetbrains.com/pycharm/help/creating-and-running-your-first-django-project.html). I use Python 3.5.1, Django 1.9.4 and Pycharm 5.0.4, the tutorial is from December 2015 and asks for at least Python 3.4.1. and Django 1.8.0 or higher.

The problem is, I guess, in the file urls.py (which is based in the polls-app folder). The content is:

from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')), #this line added
url(r'^admin/', include(admin.site.urls)),

and the error message I receive, when visiting

Using the URLconf defined in mysite.urls, Django tried these URL
> patterns, in this order:
> 1. ^polls/ ^$ [name='index']
> 2. ^admin/
> The current URL, polls/1, didn't match any of these.


The PyCharm tutorial you are following is incomplete. It only gives one url to add to polls/urls.py.

url(r'^$', views.index, name='index'),

This will handle /polls/. The tutorial does not give instructions how to handle other urls like /polls/1/.

You would be better to use the official Django tutorial, which also creates a polls app. It is kept up to date with each new Django release. You might be able to jump to this section and update the polls/urls.py by adding these patterns:

# ex: /polls/5/
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),