Django for ISchoolers: Difference between revisions

Undo revision 19296; minus URLs to avoid captcha
(YCYWXQLtXrBzDKarPRMGaVIXCqzgj)
(Undo revision 19296; minus URLs to avoid captcha)
 
Line 342:
For Django, when a regular expression matches the URL that a web surfer requests, Django extracts the captured values (if any) and passes them to a function of your choosing. This is the role of the callback function above. When a regular expression matches the url, Django calls the associated callback function with any captured parts as parameters. This will much clearer after the next section.
 
===Adding URLs to our urls.py===
price of ultram - ultram tramadol hydrochloride 50mg
 
When we ran <source lang="python">django-admin.py startproject mysite</source> to create the project, Django created a default URLconf file called `urls.py`.
 
* Write our URL mapping. Edit the file mysite/urls.py so it looks like this:
 
<source lang="python">urlpatterns = patterns('',
(r'^questions/$', 'qandabear.views.index'),
(r'^questions/(\d+)/$', 'qandabear.views.question'),
(r'^questions/(\d+)/answers/(\d+)/edit/$', 'qandabear.views.edit_answer'),
# Examples:
# url(r'^$', 'myproject.views.home', name='home'),
# url(r'^myproject/', include('myproject.foo.urls')),
 
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
 
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
)</source>
 
Suppose a visitor goes to http:// 127.0.0.1:8000/questions/23/.
# which regex pattern is tripped?
# what function is then called?
# what arguments is that function called with?
 
* Save urls.py.
 
* Start the dev server and try that url out! What happens?
 
* Save and commit.
 
Review: When somebody requests a page from your Web site – say, “/questions/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'^questions/(\d+)/$' – it loads the function question() from qandabear/views.py. Finally, it calls that module’s detail() function like so:
 
<source lang="python">detail(request=<HttpRequest object>, '23')</source>
 
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).
 
The idea that a URL doesn’t have to map onto a file or a folder, or some other sort of static resource, is quite powerful. The URL is just a way of giving instructions to some server, somewhere.
 
(Rant: In Django, as in most modern frameworks, you have total control over the way your URLs look. People on the web won’t see cruft like .py or .php or even .html at the end of your URLs. There is no excuse for that kind of stuff in the modern era! (Though, putting .asp on the end of all your Django URLs, while pointless, is kind of hilarious and super easy.))
 
== views.py ==
Anonymous user