django.conf.urls utility functionsLink para este cabeçalho

patterns()Link para este cabeçalho

patterns(prefix, pattern_description, ...)Link para esta definição

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. Here’s the example URLconf from the Django overview:

Code
from django.conf.urls import patterns, url

urlpatterns = patterns('',
    url(r'^articles/([0-9]{4})/$', 'news.views.year_archive'),
    url(r'^articles/([0-9]{4})/([0-9]{2})/$', 'news.views.month_archive'),
    url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', 'news.views.article_detail'),
)

In this example, each view has a common prefix – 'news.views'. Instead of typing that out for each entry in urlpatterns, you can use the first argument to the patterns() function to specify a prefix to apply to each view function.

With this in mind, the above example can be written more concisely as:

Code
from django.conf.urls import patterns, url

urlpatterns = patterns('news.views',
    url(r'^articles/([0-9]{4})/$', 'year_archive'),
    url(r'^articles/([0-9]{4})/([0-9]{2})/$', 'month_archive'),
    url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', 'article_detail'),
)

Note that you don’t put a trailing dot (".") in the prefix. Django puts that in automatically.

The remaining arguments should be tuples in this format:

Code
(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.

static()Link para este cabeçalho

static.static(prefix, view=django.views.static.serve, **kwargs)Link para esta definição

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

Code
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()Link para este cabeçalho

url(regex, view, kwargs=None, name=None, prefix='')Link para esta definição

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

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

This function takes five arguments, most of which are optional:

Code
url(regex, view, kwargs=None, name=None, prefix='')

The kwargs parameter allows you to pass additional arguments to the view function or method. See Passando opções extras para função “view” for an example.

See Naming URL patterns for why the name parameter is useful.

include()Link para este cabeçalho

include(module, namespace=None, app_name=None)Link para esta definição
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 application namespace and 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.

Parâmetros:
  • 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 django.conf.urls.url() instances

  • app_namespace (string) – Application namespace for the URL entries being included

  • instance_namespace (string) – Instance namespace for the URL entries being included

See Incluindo outros URLconfs and Domínios de nomes de URL e URLconfs incluídos.

handler400Link para este cabeçalho

handler400Link para esta definição

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'. If you implement a custom view, be sure it returns an HttpResponseBadRequest.

See the documentation about the 400 (bad request) view for more information.

handler403Link para este cabeçalho

handler403Link para esta definição

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'. If you implement a custom view, be sure it returns an HttpResponseForbidden.

See the documentation about the 403 (HTTP Forbidden) view for more information.

handler404Link para este cabeçalho

handler404Link para esta definição

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'. If you implement a custom view, be sure it returns an HttpResponseNotFound.

See the documentation about the 404 (HTTP Not Found) view for more information.

handler500Link para este cabeçalho

handler500Link para esta definição

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'. If you implement a custom view, be sure it returns an HttpResponseServerError.

See the documentation about the 500 (HTTP Internal Server Error) view for more information.