Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed several bugs in docs/url_dispatch.txt, and made several clarifi…

…cations

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1453 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 501b06c7b497008272ce15e47224cca982d529f2 1 parent 0cb2a00
Adrian Holovaty authored November 27, 2005

Showing 1 changed file with 29 additions and 17 deletions. Show diff stats Hide diff stats

  1. 46  docs/url_dispatch.txt
46  docs/url_dispatch.txt
@@ -56,10 +56,10 @@ Here's a sample URLconf::
56 56
     from django.conf.urls.defaults import *
57 57
 
58 58
     urlpatterns = patterns('',
59  
-        (r'^/articles/2003/$', 'news.views.special_case_2003'),
60  
-        (r'^/articles/(?P<year>\d{4})/$', 'news.views.year_archive'),
61  
-        (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'news.views.month_archive'),
62  
-        (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'news.views.article_detail'),
  59
+        (r'^articles/2003/$', 'news.views.special_case_2003'),
  60
+        (r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive'),
  61
+        (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'news.views.month_archive'),
  62
+        (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'news.views.article_detail'),
63 63
     )
64 64
 
65 65
 Notes:
@@ -71,6 +71,9 @@ Notes:
71 71
       where ``name`` is the name for that value and ``pattern`` is some pattern
72 72
       to match.
73 73
 
  74
+    * There's no need to add a leading slash, because every URL has that. For
  75
+      example, it's ``^articles``, not ``^/articles``.
  76
+
74 77
     * The ``"r"`` in front of each regular expression string is optional but
75 78
       recommended. It tells Python that a string is "raw" -- that nothing in
76 79
       the string should be escaped. See `Dive Into Python's explanation`_.
@@ -168,7 +171,7 @@ Each captured argument is sent to the view as a plain Python string, regardless
168 171
 of what sort of match the regular expression makes. For example, in this
169 172
 URLconf::
170 173
 
171  
-    (r'^/articles/(?P<year>\d{4})/$', 'news.views.year_archive'),
  174
+    (r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive'),
172 175
 
173 176
 ...the ``year`` argument to ``news.views.year_archive()`` will be a string, not
174 177
 an integer, even though the ``\d{4}`` will only match integer strings.
@@ -178,8 +181,8 @@ Here's an example URLconf and view::
178 181
 
179 182
     # URLconf
180 183
     urlpatterns = patterns('',
181  
-        (r'^/blog/$', 'blog.views.page'),
182  
-        (r'^/blog/page(?P<num>\d+)/$', 'blog.views.page'),
  184
+        (r'^blog/$', 'blog.views.page'),
  185
+        (r'^blog/page(?P<num>\d+)/$', 'blog.views.page'),
183 186
     )
184 187
 
185 188
     # View (in blog/views.py)
@@ -209,9 +212,9 @@ Here's the example URLconf from the `Django overview`_::
209 212
     from django.conf.urls.defaults import *
210 213
 
211 214
     urlpatterns = patterns('',
212  
-        (r'^/articles/(?P<year>\d{4})/$', 'myproject.news.views.year_archive'),
213  
-        (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'myproject.news.views.month_archive'),
214  
-        (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'myproject.news.views.article_detail'),
  215
+        (r'^articles/(?P<year>\d{4})/$', 'myproject.news.views.year_archive'),
  216
+        (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'myproject.news.views.month_archive'),
  217
+        (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'myproject.news.views.article_detail'),
215 218
     )
216 219
 
217 220
 In this example, each view has a common prefix -- ``"myproject.news.views"``.
@@ -224,9 +227,9 @@ With this in mind, the above example can be written more concisely as::
224 227
     from django.conf.urls.defaults import *
225 228
 
226 229
     urlpatterns = patterns('myproject.news.views',
227  
-        (r'^/articles/(?P<year>\d{4})/$', 'year_archive'),
228  
-        (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'month_archive'),
229  
-        (r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'article_detail'),
  230
+        (r'^articles/(?P<year>\d{4})/$', 'year_archive'),
  231
+        (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'month_archive'),
  232
+        (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'article_detail'),
230 233
     )
231 234
 
232 235
 Note that you don't put a trailing dot (``"."``) in the prefix. Django puts
@@ -252,8 +255,19 @@ number of other URLconfs::
252 255
         (r'^rss/',           include('django.conf.urls.rss')),
253 256
     )
254 257
 
255  
-Note that an included URLconf receives any captured parameters from parent
256  
-URLconfs, so the following example is valid::
  258
+Note that the regular expressions in this example don't have a ``$``
  259
+(end-of-string match character) but do include a trailing slash. Whenever
  260
+Django encounters ``include()``, it chops off whatever part of the URL matched
  261
+up to that point and sends the remaining string to the included URLconf for
  262
+further processing.
  263
+
  264
+.. _`Django website`: http://www.djangoproject.com/
  265
+
  266
+Captured parameters
  267
+-------------------
  268
+
  269
+An included URLconf receives any captured parameters from parent URLconfs, so
  270
+the following example is valid::
257 271
 
258 272
     # In settings/urls/main.py
259 273
     urlpatterns = patterns('',
@@ -269,8 +283,6 @@ URLconfs, so the following example is valid::
269 283
 In the above example, the captured ``"username"`` variable is passed to the
270 284
 included URLconf, as expected.
271 285
 
272  
-.. _`Django website`: http://www.djangoproject.com/
273  
-
274 286
 Passing extra options to view functions
275 287
 =======================================
276 288
 

0 notes on commit 501b06c

Please sign in to comment.
Something went wrong with that request. Please try again.