Django

Code

Changeset 2792

Show
Ignore:
Timestamp:
04/29/06 13:52:53 (2 years ago)
Author:
adrian
Message:

magic-removal: Proofread docs/url_dispatch.txt

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/docs/url_dispatch.txt

    r2638 r2792  
    3333algorithm the system follows to determine which Python code to execute: 
    3434 
    35     1. The system looks at the ``ROOT_URLCONF`` setting in your 
    36        `settings file`_. This should be a string representing the full Python 
    37        import path to your URLconf. For example: ``"mydjangoapps.urls"``. 
    38     2. The system loads that Python module and looks for the variable 
    39        ``urlpatterns``. This should be a Python list, in the format returned 
    40        by the function ``django.conf.urls.defaults.patterns()``. 
    41     3. The system runs through each URL pattern, in order, and stops at the 
    42        first one 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. 
    4343    4. Once one of the regexes matches, Django imports and calls the given 
    4444       view, which is a simple Python function. The view gets passed a 
    45        `request object`_ and any values captured in the regex as function 
    46        arguments. 
     45       `request object`_ as its first argument and any values captured in the 
     46       regex as remaining arguments. 
    4747 
    4848.. _settings file: http://www.djangoproject.com/documentation/settings/ 
     
    6565Notes: 
    6666 
    67     * ``from django.conf.urls.defaults import *`` makes the ``patterns`` 
     67    * ``from django.conf.urls.defaults import *`` makes the ``patterns()`` 
    6868      function available. 
    6969 
     
    7373      example, it's ``^articles``, not ``^/articles``. 
    7474 
    75     * The ``"r"`` in front of each regular expression string is optional but 
     75    * The ``'r'`` in front of each regular expression string is optional but 
    7676      recommended. It tells Python that a string is "raw" -- that nothing in 
    7777      the string should be escaped. See `Dive Into Python's explanation`_. 
    7878 
    79 Examples: 
     79Example requests: 
    8080 
    8181    * A request to ``/articles/2005/03/`` would match the third entry in the 
     
    122122 
    123123This accomplishes exactly the same thing as the previous example, with one 
    124 subtle difference: The captured values are passed as keyword arguments rather 
    125 than positional arguments. For example: 
     124subtle difference: The captured values are passed to view functions as keyword 
     125arguments rather than positional arguments. For example: 
    126126 
    127127    * A request to ``/articles/2005/03/`` would call the function 
     
    135135to argument-order bugs -- and you can reorder the arguments in your views' 
    136136function definitions. Of course, these benefits come at the cost of brevity; 
    137 some folks find the named-group syntax ugly and too verbose. 
     137some developers find the named-group syntax ugly and too verbose. 
    138138 
    139139The matching/grouping algorithm 
     
    160160In a request to ``http://www.example.com/myapp/?page=3``, the URLconf will look 
    161161for ``/myapp/``. 
     162 
     163The URLconf doesn't look at the request method. In other words, all request 
     164methods -- ``POST``, ``GET``, ``HEAD``, etc. -- will be routed to the same 
     165function for the same URL. 
    162166 
    163167Syntax of the urlpatterns variable 
     
    184188    (regular expression, Python callback function [, optional dictionary]) 
    185189 
    186 ...where ``dictionary_of_extra_arguments`` is optional. (See 
    187 "Passing extra options to view functions" below.) 
     190...where ``optional dictionary`` is optional. (See 
     191_`Passing extra options to view functions` below.) 
    188192 
    189193handler404 
     
    210214 
    211215A function that takes a full Python import path to another URLconf that should 
    212 be "included" in this place. See "Including other URLconfs" below. 
     216be "included" in this place. See _`Including other URLconfs` below. 
    213217 
    214218Notes on capturing text in URLs 
     
    260264 
    261265    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 
     271In this example, each view has a common prefix -- ``'myproject.news.views'``. 
    268272Instead of typing that out for each entry in ``urlpatterns``, you can use the 
    269273first argument to the ``patterns()`` function to specify a prefix to apply to 
     
    275279 
    276280    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'), 
    280284    ) 
    281285 
     
    300304        (r'^documentation/', include('django_website.apps.docs.urls.docs')), 
    301305        (r'^comments/',      include('django.contrib.comments.urls.comments')), 
    302         (r'^rss/',           include('django.conf.urls.rss')), 
    303306    ) 
    304307