2005-07-20 09:16:51 +08:00
|
|
|
==============
|
|
|
|
URL dispatcher
|
|
|
|
==============
|
2005-07-16 05:17:52 +08:00
|
|
|
|
|
|
|
We're fanatics about good URLs. No ".php" or ".cgi", and certainly not any of
|
2005-07-20 09:16:51 +08:00
|
|
|
that "0,2097,1-1-1928,00" nonsense. Django's URL dispatcher lets you design
|
2005-07-16 05:17:52 +08:00
|
|
|
your URLs to be as pretty as the rest of your application.
|
|
|
|
|
2005-07-20 09:16:51 +08:00
|
|
|
See `the Django overview`_ for a quick introduction to URL configurations; this
|
|
|
|
document will continue from there.
|
2005-07-16 05:17:52 +08:00
|
|
|
|
|
|
|
.. _`the Django overview`: http://www.djangoproject.com/documentation/overview/#design-your-urls
|
|
|
|
|
|
|
|
The view prefix
|
|
|
|
===============
|
|
|
|
|
|
|
|
Here's the example from that overview::
|
|
|
|
|
|
|
|
from django.conf.urls.defaults import *
|
2005-07-20 09:16:51 +08:00
|
|
|
|
2005-07-16 05:17:52 +08:00
|
|
|
urlpatterns = patterns('',
|
2005-07-20 09:16:51 +08:00
|
|
|
(r'^/articles/(?P<year>\d{4})/$', 'myproject.news.views.articles.year_archive'),
|
|
|
|
(r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'myproject.news.views.articles.month_archive'),
|
|
|
|
(r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'myproject.news.views.articles.article_detail'),
|
2005-07-16 05:17:52 +08:00
|
|
|
)
|
2005-07-20 09:16:51 +08:00
|
|
|
|
2005-07-16 05:17:52 +08:00
|
|
|
You can see that the first argument to ``patterns`` is an empty string in the
|
2005-07-20 09:16:51 +08:00
|
|
|
above example, but that argument is actually very useful. The first argument
|
|
|
|
will be prepended to all the view functions in the urlpatterns list, so the
|
|
|
|
above example could be written more concisely as::
|
2005-07-16 05:17:52 +08:00
|
|
|
|
|
|
|
urlpatterns = patterns('myproject.news.views.articles',
|
2005-07-20 09:16:51 +08:00
|
|
|
(r'^/articles/(?P<year>\d{4})/$', 'year_archive'),
|
|
|
|
(r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'month_archive'),
|
|
|
|
(r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'article_detail'),
|
2005-07-16 05:17:52 +08:00
|
|
|
)
|
|
|
|
|
2005-07-22 02:12:29 +08:00
|
|
|
.. admonition:: Note
|
|
|
|
|
2005-07-22 03:45:36 +08:00
|
|
|
More precisely, the actual view function used is ``prefix + "." +
|
|
|
|
function_name``. The trailing "dot" does not need to be put in the prefix.
|
2005-07-22 02:12:29 +08:00
|
|
|
|
2005-07-20 09:16:51 +08:00
|
|
|
Including other URLconfs
|
|
|
|
========================
|
2005-07-16 05:17:52 +08:00
|
|
|
|
2005-07-20 09:16:51 +08:00
|
|
|
You can also "include" other URL config modules at any point along the path.
|
|
|
|
This essentially "roots" a set of URLs below other ones. This is most often
|
|
|
|
used for a site's "base" URLconfig; the ``ROOT_URLCONF`` setting points to a
|
|
|
|
urlconf module that will be used for the entire site. Here's the URLconf
|
|
|
|
for the `Django website`_ itself. It includes a number of other URLconfs::
|
2005-07-16 05:17:52 +08:00
|
|
|
|
|
|
|
from django.conf.urls.defaults import *
|
2005-07-20 09:16:51 +08:00
|
|
|
|
2005-07-16 05:17:52 +08:00
|
|
|
urlpatterns = patterns('',
|
|
|
|
(r'^weblog/', include('django_website.apps.blog.urls.blog')),
|
|
|
|
(r'^documentation/', include('django_website.apps.docs.urls.docs')),
|
|
|
|
(r'^comments/', include('django.contrib.comments.urls.comments')),
|
|
|
|
(r'^rss/', include('django.conf.urls.rss')),
|
|
|
|
(r'', include('django.conf.urls.flatfiles')),
|
|
|
|
)
|
2005-07-20 09:16:51 +08:00
|
|
|
|
2005-07-16 05:17:52 +08:00
|
|
|
.. _`Django website`: http://www.djangoproject.com/
|
|
|
|
|
|
|
|
Passing extra options to view functions
|
|
|
|
=======================================
|
|
|
|
|
|
|
|
There are two ways of passing arguments into your view functions: named captures
|
|
|
|
from the regex (which you've already seen) and the optional third element
|
2005-07-20 09:16:51 +08:00
|
|
|
in URLconf tuples. This third element can be a dictionary of extra keyword
|
|
|
|
arguments that will be passed to the view function::
|
2005-07-16 05:17:52 +08:00
|
|
|
|
|
|
|
urlpatterns = patterns('myproject.news.views.articles',
|
2005-07-20 09:16:51 +08:00
|
|
|
(r'^/articles/(?P<year>\d{4})/$', 'year_archive', {key: value, key2: value 2}),
|
2005-07-16 05:17:52 +08:00
|
|
|
)
|