Anonymous user
Boston Python Workshop/Saturday/Web app project: Difference between revisions
Boston Python Workshop/Saturday/Web app project (view source)
Revision as of 12:30, 5 March 2011
, 13 years ago→Adding URLs to urls.py
imported>Paulproteus |
imported>Paulproteus |
||
Line 415:
=== Adding URLs to urls.py ===
When we ran django-admin.py startproject workshop_mysite to create the project, Django created a default URLconf
ROOT_URLCONF = 'workshop_mysite.urls'
That means that the default URLconf in workshop_mysite/urls.py.
Time for an example. Edit mysite/urls.py so it looks like this:
Line 432 ⟶ 434:
This is worth a review. When somebody requests a page from your Web site -- say, "/polls/23/", Django will load the ''urls.py'' Python module, because it's pointed to by the ROOT_URLCONF setting. It finds the variable named urlpatterns and traverses the regular expressions in order. When it finds a regular expression that matches -- r'^polls/(?P<poll_id>\d+)/$' -- it loads the function detail() from polls/views.py. Finally, it calls that detail() function like so:
detail(request=<HttpRequest object>, '23')
The '23' part comes from (\d+). Using parentheses around a pattern "captures" the text matched by that pattern and sends it as an argument to the view function; the \d+ is a regular expression to match a sequence of ''digits'' (i.e., a number).
|