diff -r e70cbd83611a docs/topics/http/urls.txt
a
|
b
|
|
3 | 3 | ============== |
4 | 4 | URL dispatcher |
5 | 5 | ============== |
| 6 | |
| 7 | .. module:: django.core.urlresolvers |
6 | 8 | |
7 | 9 | A clean, elegant URL scheme is an important detail in a high-quality Web |
8 | 10 | application. Django lets you design URLs however you want, with no framework |
… |
… |
|
182 | 184 | patterns |
183 | 185 | -------- |
184 | 186 | |
| 187 | .. function:: patterns(prefix, pattern_description, ...) |
| 188 | |
185 | 189 | A function that takes a prefix, and an arbitrary number of URL patterns, and |
186 | 190 | returns a list of URL patterns in the format Django needs. |
187 | 191 | |
188 | 192 | The first argument to ``patterns()`` is a string ``prefix``. See |
189 | | "The view prefix" below. |
| 193 | `The view prefix`_ below. |
190 | 194 | |
191 | 195 | The remaining arguments should be tuples in this format:: |
192 | 196 | |
… |
… |
|
222 | 226 | |
223 | 227 | .. versionadded:: 1.0 |
224 | 228 | |
| 229 | .. function:: url(regex, view, kwargs=None, name=None, prefix='') |
| 230 | |
225 | 231 | You can use the ``url()`` function, instead of a tuple, as an argument to |
226 | 232 | ``patterns()``. This is convenient if you want to specify a name without the |
227 | 233 | optional extra arguments dictionary. For example:: |
… |
… |
|
244 | 250 | handler404 |
245 | 251 | ---------- |
246 | 252 | |
| 253 | .. data:: handler404 |
| 254 | |
247 | 255 | A string representing the full Python import path to the view that should be |
248 | 256 | called if none of the URL patterns match. |
249 | 257 | |
… |
… |
|
252 | 260 | |
253 | 261 | handler500 |
254 | 262 | ---------- |
| 263 | |
| 264 | .. data:: handler500 |
255 | 265 | |
256 | 266 | A string representing the full Python import path to the view that should be |
257 | 267 | called in case of server errors. Server errors happen when you have runtime |
… |
… |
|
263 | 273 | include |
264 | 274 | ------- |
265 | 275 | |
| 276 | .. function:: include(<module or pattern_list>) |
| 277 | |
266 | 278 | A function that takes a full Python import path to another URLconf module that |
267 | 279 | should be "included" in this place. |
268 | 280 | |
269 | 281 | .. versionadded:: 1.1 |
270 | 282 | |
271 | | :meth:``include`` also accepts as an argument an iterable that returns URL |
| 283 | :func:`include` also accepts as an argument an iterable that returns URL |
272 | 284 | patterns. |
273 | 285 | |
274 | 286 | See `Including other URLconfs`_ below. |
… |
… |
|
421 | 433 | Admin application. The Django Admin is deployed as instances of a |
422 | 434 | :class:`AdminSite`; each :class:`AdminSite` instance has an attribute |
423 | 435 | ``urls`` that returns the url patterns available to that instance. It is this |
424 | | attribute that you ``included()`` into your projects ``urlpatterns`` when you |
| 436 | attribute that you ``include()`` into your projects ``urlpatterns`` when you |
425 | 437 | deploy the admin instance. |
426 | 438 | |
427 | 439 | .. _`Django Web site`: http://www.djangoproject.com/ |
… |
… |
|
451 | 463 | Defining URL Namespaces |
452 | 464 | ----------------------- |
453 | 465 | |
454 | | When you need to deploying multiple instances of a single application, it can |
| 466 | When you need to deploy multiple instances of a single application, it can |
455 | 467 | be helpful to be able to differentiate between instances. This is especially |
456 | 468 | important when using _`named URL patterns <naming-url-patterns>`, since |
457 | 469 | multiple instances of a single application will share named URLs. Namespaces |
… |
… |
|
466 | 478 | |
467 | 479 | * An **instance namespace**. This identifies a specific instance of an |
468 | 480 | application. Instance namespaces should be unique across your entire |
469 | | project. However, and instance namespace can be the same as the |
| 481 | project. However, an instance namespace can be the same as the |
470 | 482 | application namespace. This is used to specify a default instance of an |
471 | 483 | application. For example, the default Django Admin instance has an |
472 | 484 | instance namespace of ``admin``. |
473 | 485 | |
474 | 486 | URL Namespaces can be specified in two ways. |
475 | 487 | |
476 | | Firstly, you can provide the applicaiton and instance namespace as arguments |
477 | | to the ``include()`` when you construct your URL patterns. For example,:: |
| 488 | Firstly, you can provide the application and instance namespace as arguments |
| 489 | to ``include()`` when you construct your URL patterns. For example,:: |
478 | 490 | |
479 | 491 | (r'^help/', include('apps.help.urls', namespace='foo', app_name='bar')), |
480 | 492 | |
… |
… |
|
494 | 506 | an admin site, plus the name of the admin instance, and the application |
495 | 507 | namespace ``admin``. |
496 | 508 | |
497 | | Once you have defined namespace URLs, you can reverse them. For details on |
| 509 | Once you have defined namespaced URLs, you can reverse them. For details on |
498 | 510 | reversing namespaced urls, see the documentation on :ref:`reversing namespaced |
499 | 511 | URLs <topics-http-reversing-url-namespaces>`. |
500 | 512 | |
… |
… |
|
679 | 691 | |
680 | 692 | .. versionadded:: 1.1 |
681 | 693 | |
682 | | Namespaced URLs are specified using the `:` operator. For example, the main index |
683 | | page of the admin application is referenced using ``admin:index``. This indicates |
684 | | a namespace of ``admin``, and a named URL of ``index``. |
| 694 | Namespaced URLs are specified using the ``:`` operator. For example, the main |
| 695 | index page of the admin application is referenced using ``admin:index``. This |
| 696 | indicates a namespace of ``admin``, and a named URL of ``index``. |
685 | 697 | |
686 | 698 | Namespaces can also be nested. The named URL ``foo:bar:whiz`` would look for |
687 | 699 | a pattern named ``whiz`` in the namespace ``bar`` that is itself defined within |
688 | 700 | the top-level namespace ``foo``. |
689 | 701 | |
690 | | When given a namespaced URL (e.g.,, `myapp:index`) to resolve, Django splits |
| 702 | When given a namespaced URL (e.g. ``myapp:index``) to resolve, Django splits |
691 | 703 | the fully qualified name into parts, and then tries the following lookup: |
692 | 704 | |
693 | | 1. Django then looks for a matching application namespace (in this |
| 705 | 1. It looks for a matching application namespace (in this |
694 | 706 | example, ``myapp``). This will yield a list of instances of that |
695 | 707 | application. |
696 | 708 | |
… |
… |
|
702 | 714 | template. |
703 | 715 | |
704 | 716 | The current application can also be specified manually as an argument |
705 | | to the :method:``reverse()`` function. |
| 717 | to the :func:`reverse()` function. |
706 | 718 | |
707 | 719 | 3. If there is no current application. Django looks for a default |
708 | 720 | application instance. The default application instance is the instance |
709 | 721 | that has an instance namespace matching the application namespace (in |
710 | | this example, an instance of the ``myapp`` called ``myapp``) |
| 722 | this example, an instance of the ``myapp`` called ``myapp``). |
711 | 723 | |
712 | 724 | 4. If there is no default application instance, Django will pick the first |
713 | | deployed instance of the application, whatever it's instance name may be. |
| 725 | deployed instance of the application, whatever its instance name may be. |
714 | 726 | |
715 | 727 | 5. If the provided namespace doesn't match an application namespace in |
716 | 728 | step 2, Django will attempt a direct lookup of the namespace as an |
… |
… |
|
762 | 774 | your code, Django provides the following method (in the |
763 | 775 | ``django.core.urlresolvers`` module): |
764 | 776 | |
765 | | .. currentmodule:: django.core.urlresolvers |
766 | 777 | .. function:: reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None) |
767 | 778 | |
768 | 779 | ``viewname`` is either the function name (either a function reference, or the |
… |
… |
|
812 | 823 | The :func:`django.core.urlresolvers.resolve` function can be used for resolving |
813 | 824 | URL paths to the corresponding view functions. It has the following signature: |
814 | 825 | |
815 | | .. currentmodule:: django.core.urlresolvers |
816 | 826 | .. function:: resolve(path, urlconf=None) |
817 | 827 | |
818 | 828 | ``path`` is the URL path you want to resolve. As with ``reverse()`` above, you |