django.conf.urls utility functions¶
Until Django 1.3 they were located in django.conf.urls.defaults. You still can import them from there but it will be removed in Django 1.6.
- patterns(prefix, pattern_description, ...)¶
A function that takes a prefix, and an arbitrary number of URL patterns, and returns a list of URL patterns in the format Django needs.
The first argument to patterns() is a string prefix. See The view prefix.
The remaining arguments should be tuples in this format:
(regular expression, Python callback function [, optional_dictionary [, optional_name]])
The optional_dictionary and optional_name parameters are described in Passing extra options to view functions.
Because patterns() is a function call, it accepts a maximum of 255 arguments (URL patterns, in this case). This is a limit for all Python function calls. This is rarely a problem in practice, because you’ll typically structure your URL patterns modularly by using include() sections. However, on the off-chance you do hit the 255-argument limit, realize that patterns() returns a Python list, so you can split up the construction of the list.
urlpatterns = patterns('', ... ) urlpatterns += patterns('', ... )
Python lists have unlimited size, so there’s no limit to how many URL patterns you can construct. The only limit is that you can only create 254 at a time (the 255th argument is the initial prefix argument).
- 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 = patterns('', # ... the rest of your URLconf goes here ... ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
- url(regex, view, kwargs=None, name=None, prefix='')¶
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='')
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.
- include(module[, namespace=None, app_name=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 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.
- module – URLconf module (or module name)
- namespace (string) – Instance namespace for the URL entries being included
- app_name (string) – Application namespace for the URL entries being included
- pattern_list – Iterable of URL entries as returned by patterns()
- app_namespace (string) – Application namespace for the URL entries being included
- instance_namespace (string) – Instance namespace for the URL entries being included
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.
Having trouble? We'd like to help!
- Try the FAQ — it's got answers to many common questions.
- Search for information in the archives of the django-users mailing list, or post a question.
- Ask a question in the #django IRC channel, or search the IRC logs to see if it has been asked before.
- If you notice errors with this documentation, please open a ticket and let us know! Please only use the ticket tracker for criticisms and improvements on the docs. For tech support, use the resources above.