Changeset 236
- Timestamp:
- 07/19/05 20:16:51 (3 years ago)
- Files:
-
- django/trunk/docs/url_dispatch.txt (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/docs/url_dispatch.txt
r70 r236 1 ============ 2 URL dispatch 3 ============ 1 ============== 2 URL dispatcher 3 ============== 4 4 5 5 We're fanatics about good URLs. No ".php" or ".cgi", and certainly not any of 6 that "0,2097,1-1-1928,00" nonsense. Django's URL dispatchlets you design6 that "0,2097,1-1-1928,00" nonsense. Django's URL dispatcher lets you design 7 7 your URLs to be as pretty as the rest of your application. 8 8 9 See `the Django overview`_ for a quick introduction to URL dispatch; this10 document will continue onfrom there.9 See `the Django overview`_ for a quick introduction to URL configurations; this 10 document will continue from there. 11 11 12 12 .. _`the Django overview`: http://www.djangoproject.com/documentation/overview/#design-your-urls … … 18 18 19 19 from django.conf.urls.defaults import * 20 20 21 21 urlpatterns = patterns('', 22 (r'^/articles/(?P \d{4})/$','myproject.news.views.articles.year_archive'),23 (r'^/articles/(?P \d{4})/(?P\d{2})/$','myproject.news.views.articles.month_archive'),24 (r'^/articles/(?P \d{4})/(?P\d{2})/$','myproject.news.views.articles.month_archive'),25 (r'^/articles/(?P \d{4})/(?P\d{2})/(?P\d+)/$', 'myproject.news.views.articles.article_detail'),22 (r'^/articles/(?P<year>\d{4})/$', 'myproject.news.views.articles.year_archive'), 23 (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'myproject.news.views.articles.month_archive'), 24 (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'myproject.news.views.articles.month_archive'), 25 (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'myproject.news.views.articles.article_detail'), 26 26 ) 27 27 28 28 You can see that the first argument to ``patterns`` is an empty string in the 29 above example, but it's actually very useful. The first argument will be30 prepended to all the view functions in the urlpatterns list, so the above 31 example could be written more concisely as::29 above example, but that argument is actually very useful. The first argument 30 will be prepended to all the view functions in the urlpatterns list, so the 31 above example could be written more concisely as:: 32 32 33 33 urlpatterns = patterns('myproject.news.views.articles', 34 (r'^/articles/(?P \d{4})/$','year_archive'),35 (r'^/articles/(?P \d{4})/(?P\d{2})/$','month_archive'),36 (r'^/articles/(?P \d{4})/(?P\d{2})/$','month_archive'),37 (r'^/articles/(?P \d{4})/(?P\d{2})/(?P\d+)/$', 'article_detail'),34 (r'^/articles/(?P<year>\d{4})/$', 'year_archive'), 35 (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'month_archive'), 36 (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'month_archive'), 37 (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'article_detail'), 38 38 ) 39 39 40 Including other URL configs41 ======================== ===40 Including other URLconfs 41 ======================== 42 42 43 You can also "include" other URL config modules at any point along the path. 44 This is essence "roots" a set of URLs below other ones. This is most often used45 for a site's "base" URL config; the ``ROOT_URLCONF`` setting points to a urlconf 46 module that will be used for the entire site. This is the URL config for the 47 `Django website`_ itself which includes a number of other URL config modules::43 You can also "include" other URL config modules at any point along the path. 44 This essentially "roots" a set of URLs below other ones. This is most often 45 used for a site's "base" URLconfig; the ``ROOT_URLCONF`` setting points to a 46 urlconf module that will be used for the entire site. Here's the URLconf 47 for the `Django website`_ itself. It includes a number of other URLconfs:: 48 48 49 49 from django.conf.urls.defaults import * 50 50 51 51 urlpatterns = patterns('', 52 52 (r'^weblog/', include('django_website.apps.blog.urls.blog')), … … 56 56 (r'', include('django.conf.urls.flatfiles')), 57 57 ) 58 58 59 59 .. _`Django website`: http://www.djangoproject.com/ 60 60 … … 64 64 There are two ways of passing arguments into your view functions: named captures 65 65 from the regex (which you've already seen) and the optional third element 66 in url config tuples. This third element can be a dictionary of extra67 keywordarguments that will be passed to the view function::66 in URLconf tuples. This third element can be a dictionary of extra keyword 67 arguments that will be passed to the view function:: 68 68 69 69 urlpatterns = patterns('myproject.news.views.articles', 70 (r'^/articles/(?P \d{4})/$', 'year_archive', {key: value, key2: value 2}),70 (r'^/articles/(?P<year>\d{4})/$', 'year_archive', {key: value, key2: value 2}), 71 71 ) 72 73 This is especially useful for `generic view functions`_.74 75 .. _`generic view functions`: http://www.djangoproject.com/documentation/generic_views/
