Une fonction acceptant un préfixe et un nombre arbitraire de motifs d’URL, et renvoyant une liste de motifs d’URL au format attendu par Django.
Le premier paramètre de patterns() est la chaîne prefix. Voir le préfixe de vue.
Les paramètres restants doivent être des tuples dans ce format :
(regular expression, Python callback function [, optional_dictionary [, optional_name]])
Les paramètres optional_dictionary et optional_name sont décrits dans Transmission de paramètres supplémentaires à une vue.
Note
Comme patterns() est un appel de fonction, elle accepte un maximum de 255 paramètres (de motifs d’URL dans ce cas). Cette limite s’applique à tous les appels de fonctions Python. C’est rarement un problème en pratique, parce que les motifs d’URL sont généralement structurés de façon modulaire avec plusieurs sections include(). Cependant, dans l’hypothèse où vous seriez tout de même confronté à la limite des 255 paramètres, sachez que patterns() renvoie une liste Python, il est donc toujours possible de segmenter la construction de la liste.
urlpatterns = patterns('',
...
)
urlpatterns += patterns('',
...
)
Les listes Python ne sont pas limitées en taille, il n’y a donc aucune limite au nombre de motifs d’URL qu’il est possible de construire. La seule limite est que vous ne pouvez en créer que 254 à la fois (le 255e paramètre est le premier paramètre du préfixe).
Fonction utilitaire qui renvoie un motif d’URL pour servir les fichiers en mode débogage :
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
You can use the url() function, instead of a tuple, as an argument to patterns(). This is convenient if you want to specify a name without the optional extra arguments dictionary. For example:
urlpatterns = patterns('',
url(r'^index/$', index_view, name="main-view"),
...
)
This function takes five arguments, most of which are optional:
url(regex, view, kwargs=None, name=None, prefix='')
The kwargs parameter allows you to pass additional arguments to the view function or method. See Transmission de paramètres supplémentaires à une vue for an example.
See Naming URL patterns for why the name parameter is useful.
The prefix parameter has the same meaning as the first argument to patterns() and is only relevant when you’re passing a string as the view parameter.
A function that takes a full Python import path to another URLconf module that should be “included” in this place. Optionally, the application namespace and instance namespace where the entries will be included into can also be specified.
include() also accepts as an argument either an iterable that returns URL patterns or a 3-tuple containing such iterable plus the names of the application and instance namespaces.
Paramètres: |
|
---|
See Inclusion d’autres URLconfs and Espaces de noms d’URL et configurations d’URL incluses.
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 the 400 (bad request) view for more information.
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 the 403 (HTTP Forbidden) view for more information.
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 the 404 (HTTP Not Found) view for more information.
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 the 500 (HTTP Internal Server Error) view for more information.
Jan 13, 2016