Reputation: 4050
I'm trying to understand this line: url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
from Django's tutorial on how to create views.
In particular, I don't understand the following:
urls.py
urlpatterns = patterns('',
# ex: /polls/
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
Upvotes: 6
Views: 3937
Reputation: 1122532
(?P<poll_id>...)
creates a named group; you can now refer to whatever was matched in that group by name.
The view will be passed a keyword parameter by that name when called.
\d
is a character group, it matches numeric digits (0
through to 9
for ASCII data). The +
is a quantifier; only 1 or more digits will match.
name='detail'
names the URL pattern so you can refer to it by name later when creating reverse URLs. See Naming URL patterns in the Django manual.
All in all, that pattern matches a URL that starts with digits, followed by just a /
slash, causing Django to call the views.detail
view, passing in the matched digits as the poll_id
parameter. The name
keyword makes it easy to generate a URL to this view:
{% url 'name' poll.id %}
would generate a URL to http://yourserver/[digits of poll.id]/
.
Upvotes: 9