Boston Python Workshop/Saturday/Web app project: Difference between revisions

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 in workshop_mysite/urls.py. ItTake alsoa automaticallylook set your ROOT_URLCONF setting (inat '''settings.py)''' to point atfor thatthis fileline:
 
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).
Anonymous user