Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/docs/ref/urls.txt
blob: c54b1e4cab95cb9bdff96f99bb237a6f69efb986 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
======================================
``django.conf.urls`` utility functions
======================================

.. module:: django.conf.urls

static()
--------

.. function:: static.static(prefix, view=django.views.static.serve, **kwargs)

Helper function to return a URL pattern for serving files in debug mode::

    from django.conf import settings
    from django.conf.urls.static import static

    urlpatterns = [
        # ... the rest of your URLconf goes here ...
    ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

url()
-----

.. function:: url(regex, view, kwargs=None, name=None)

``urlpatterns`` should be a list of ``url()`` instances. For example::

    urlpatterns = [
        url(r'^index/$', index_view, name="main-view"),
        ...
    ]

The ``kwargs`` parameter allows you to pass additional arguments to the view
function or method. See :ref:`views-extra-options` for an example.

See :ref:`Naming URL patterns <naming-url-patterns>` for why the ``name``
parameter is useful.

include()
---------

.. function:: include(module, namespace=None, app_name=None)
              include(pattern_list)
              include((pattern_list, app_namespace), namespace=None)
              include((pattern_list, app_namespace, instance_namespace))

    A function that takes a full Python import path to another URLconf module
    that should be "included" in this place. Optionally, the :term:`application
    namespace` and :term:`instance namespace` where the entries will be included
    into can also be specified.

    Usually, the application namespace should be specified by the included
    module. If an application namespace is set, the ``namespace`` argument
    can be used to set a different instance namespace.

    ``include()`` also accepts as an argument either an iterable that returns
    URL patterns, a 2-tuple containing such iterable plus the names of the
    application namespaces, or a 3-tuple containing the iterable and the names
    of both the application and instance namespace.

    :arg module: URLconf module (or module name)
    :arg namespace: Instance namespace for the URL entries being included
    :type namespace: string
    :arg app_name: Application namespace for the URL entries being included
    :type app_name: string
    :arg pattern_list: Iterable of :func:`django.conf.urls.url` instances
    :arg app_namespace: Application namespace for the URL entries being included
    :type app_namespace: string
    :arg instance_namespace: Instance namespace for the URL entries being included
    :type instance_namespace: string

See :ref:`including-other-urlconfs` and :ref:`namespaces-and-include`.

.. deprecated:: 1.9

    Support for the ``app_name`` argument is deprecated and will be removed in
    Django 2.0. Specify the ``app_name`` as explained in
    :ref:`namespaces-and-include` instead.

    Support for passing a 3-tuple is also deprecated and will be removed in
    Django 2.0. Pass a 2-tuple containing the pattern list and application
    namespace, and use the ``namespace`` argument instead.

    Lastly, support for an instance namespace without an application namespace
    has been deprecated and will be removed in Django 2.0. Specify the
    application namespace or remove the instance namespace.

handler400
----------

.. data:: handler400

A callable, or a string representing the full Python import path to the view
that should be called if the HTTP client has sent a request that caused an error
condition and a response with a status code of 400.

By default, this is ``'django.views.defaults.bad_request'``. That default
value should suffice.

See the documentation about :ref:`the 400 (bad request) view
<http_bad_request_view>` for more information.

handler403
----------

.. data:: handler403

A callable, or a string representing the full Python import path to the view
that should be called if the user doesn't have the permissions required to
access a resource.

By default, this is ``'django.views.defaults.permission_denied'``. That default
value should suffice.

See the documentation about :ref:`the 403 (HTTP Forbidden) view
<http_forbidden_view>` for more information.

handler404
----------

.. data:: handler404

A callable, or a string representing the full Python import path to the view
that should be called if none of the URL patterns match.

By default, this is ``'django.views.defaults.page_not_found'``. That default
value should suffice.

See the documentation about :ref:`the 404 (HTTP Not Found) view
<http_not_found_view>` for more information.

handler500
----------

.. data:: handler500

A callable, or a string representing the full Python import path to the view
that should be called in case of server errors. Server errors happen when you
have runtime errors in view code.

By default, this is ``'django.views.defaults.server_error'``. That default
value should suffice.

See the documentation about :ref:`the 500 (HTTP Internal Server Error) view
<http_internal_server_error_view>` for more information.