Changeset 2792
- Timestamp:
- 04/29/06 13:52:53 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/magic-removal/docs/url_dispatch.txt
r2638 r2792 33 33 algorithm the system follows to determine which Python code to execute: 34 34 35 1. The system looks at the ``ROOT_URLCONF`` setting in your36 `settings file`_. This should be a string representing the full Python37 import path to yourURLconf. For example: ``"mydjangoapps.urls"``.38 2. The systemloads that Python module and looks for the variable39 ``urlpatterns``. This should be a Python list, in the format returned 40 bythe function ``django.conf.urls.defaults.patterns()``.41 3. The system runs through each URL pattern, in order, and stops at the42 firstone that matches the requested URL.35 1. Django looks at the ``ROOT_URLCONF`` setting in your `settings file`_. 36 This should be a string representing the full Python import path to your 37 URLconf. For example: ``"mydjangoapps.urls"``. 38 2. Django loads that Python module and looks for the variable 39 ``urlpatterns``. This should be a Python list, in the format returned by 40 the function ``django.conf.urls.defaults.patterns()``. 41 3. Django runs through each URL pattern, in order, and stops at the first 42 one that matches the requested URL. 43 43 4. Once one of the regexes matches, Django imports and calls the given 44 44 view, which is a simple Python function. The view gets passed a 45 `request object`_ a nd any values captured in the regex as function46 arguments.45 `request object`_ as its first argument and any values captured in the 46 regex as remaining arguments. 47 47 48 48 .. _settings file: http://www.djangoproject.com/documentation/settings/ … … 65 65 Notes: 66 66 67 * ``from django.conf.urls.defaults import *`` makes the ``patterns ``67 * ``from django.conf.urls.defaults import *`` makes the ``patterns()`` 68 68 function available. 69 69 … … 73 73 example, it's ``^articles``, not ``^/articles``. 74 74 75 * The `` "r"`` in front of each regular expression string is optional but75 * The ``'r'`` in front of each regular expression string is optional but 76 76 recommended. It tells Python that a string is "raw" -- that nothing in 77 77 the string should be escaped. See `Dive Into Python's explanation`_. 78 78 79 Example s:79 Example requests: 80 80 81 81 * A request to ``/articles/2005/03/`` would match the third entry in the … … 122 122 123 123 This accomplishes exactly the same thing as the previous example, with one 124 subtle difference: The captured values are passed as keyword arguments rather125 than positional arguments. For example:124 subtle difference: The captured values are passed to view functions as keyword 125 arguments rather than positional arguments. For example: 126 126 127 127 * A request to ``/articles/2005/03/`` would call the function … … 135 135 to argument-order bugs -- and you can reorder the arguments in your views' 136 136 function definitions. Of course, these benefits come at the cost of brevity; 137 some folks find the named-group syntax ugly and too verbose.137 some developers find the named-group syntax ugly and too verbose. 138 138 139 139 The matching/grouping algorithm … … 160 160 In a request to ``http://www.example.com/myapp/?page=3``, the URLconf will look 161 161 for ``/myapp/``. 162 163 The URLconf doesn't look at the request method. In other words, all request 164 methods -- ``POST``, ``GET``, ``HEAD``, etc. -- will be routed to the same 165 function for the same URL. 162 166 163 167 Syntax of the urlpatterns variable … … 184 188 (regular expression, Python callback function [, optional dictionary]) 185 189 186 ...where `` dictionary_of_extra_arguments`` is optional. (See187 "Passing extra options to view functions"below.)190 ...where ``optional dictionary`` is optional. (See 191 _`Passing extra options to view functions` below.) 188 192 189 193 handler404 … … 210 214 211 215 A function that takes a full Python import path to another URLconf that should 212 be "included" in this place. See "Including other URLconfs"below.216 be "included" in this place. See _`Including other URLconfs` below. 213 217 214 218 Notes on capturing text in URLs … … 260 264 261 265 urlpatterns = patterns('', 262 (r'^articles/( ?P<year>\d{4})/$', 'myproject.news.views.year_archive'),263 (r'^articles/( ?P<year>\d{4})/(?P<month>\d{2})/$', 'myproject.news.views.month_archive'),264 (r'^articles/( ?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'myproject.news.views.article_detail'),265 ) 266 267 In this example, each view has a common prefix -- `` "myproject.news.views"``.266 (r'^articles/(\d{4})/$', 'myproject.news.views.year_archive'), 267 (r'^articles/(\d{4})/(\d{2})/$', 'myproject.news.views.month_archive'), 268 (r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'myproject.news.views.article_detail'), 269 ) 270 271 In this example, each view has a common prefix -- ``'myproject.news.views'``. 268 272 Instead of typing that out for each entry in ``urlpatterns``, you can use the 269 273 first argument to the ``patterns()`` function to specify a prefix to apply to … … 275 279 276 280 urlpatterns = patterns('myproject.news.views', 277 (r'^articles/( ?P<year>\d{4})/$', 'year_archive'),278 (r'^articles/( ?P<year>\d{4})/(?P<month>\d{2})/$', 'month_archive'),279 (r'^articles/( ?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'article_detail'),281 (r'^articles/(\d{4})/$', 'year_archive'), 282 (r'^articles/(\d{4})/(\d{2})/$', 'month_archive'), 283 (r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'article_detail'), 280 284 ) 281 285 … … 300 304 (r'^documentation/', include('django_website.apps.docs.urls.docs')), 301 305 (r'^comments/', include('django.contrib.comments.urls.comments')), 302 (r'^rss/', include('django.conf.urls.rss')),303 306 ) 304 307
