django.urls functions for use in URLconfs

path()

  • path(route, view, kwargs=None, name=None)
  • Returns an element for inclusion in urlpatterns. For example:
  1. from django.urls import include, path
  2.  
  3. urlpatterns = [
  4. path('index/', views.index, name='main-view'),
  5. path('bio/<username>/', views.bio, name='bio'),
  6. path('articles/<slug:title>/', views.article, name='article-detail'),
  7. path('articles/<slug:title>/<int:section>/', views.section, name='article-section'),
  8. path('weblog/', include('blog.urls')),
  9. ...
  10. ]

The route argument should be a string orgettext_lazy() (seeTranslating URL patterns) that contains a URL pattern. The stringmay contain angle brackets (like <username> above) to capture part of theURL and send it as a keyword argument to the view. The angle brackets mayinclude a converter specification (like the int part of <int:section>)which limits the characters matched and may also change the type of thevariable passed to the view. For example, <int:section> matches a stringof decimal digits and converts the value to an int. SeeDjango 如何处理一个请求 for more details.

The view argument is a view function or the result ofas_view() for class-based views. It canalso be an django.urls.include().

The kwargs argument allows you to pass additional arguments to the viewfunction or method. See Passing extra options to view functions for an example.

See Naming URL patterns for why the nameargument is useful.

re_path()

  • repath(_route, view, kwargs=None, name=None)
  • Returns an element for inclusion in urlpatterns. For example:
  1. from django.urls import include, re_path
  2.  
  3. urlpatterns = [
  4. re_path(r'^index/$', views.index, name='index'),
  5. re_path(r'^bio/(?P<username>\w+)/$', views.bio, name='bio'),
  6. re_path(r'^weblog/', include('blog.urls')),
  7. ...
  8. ]

The route argument should be a string orgettext_lazy() (seeTranslating URL patterns) that contains a regular expression compatiblewith Python's re module. Strings typically use raw string syntax(r'') so that they can contain sequences like \d without the need toescape the backslash with another backslash. When a match is made, capturedgroups from the regular expression are passed to the view — as named argumentsif the groups are named, and as positional arguments otherwise. The values arepassed as strings, without any type conversion.

The view, kwargs and name arguments are the same as forpath().

include()

  • include(module, namespace=None)[源代码]
  • include(pattern_list)
  • include((pattern_list, app_namespace), namespace=None)
  • A function that takes a full Python import path to another URLconf modulethat should be "included" in this place. Optionally, the applicationnamespace and instance namespace where the entries will be includedinto can also be specified.

Usually, the application namespace should be specified by the includedmodule. If an application namespace is set, the namespace argumentcan be used to set a different instance namespace.

include() also accepts as an argument either an iterable that returnsURL patterns or a 2-tuple containing such iterable plus the names of theapplication namespaces.

参数:

  • module — URLconf module (or module name)
  • namespace (str) — Instance namespace for the URL entries being included
  • pattern_list — Iterable of path() and/or re_path() instances.
  • app_namespace (str) — Application namespace for the URL entries being included

See 包含其它的URLconfs and URL namespaces and included URLconfs.

register_converter()

  • registerconverter(_converter, type_name)[源代码]
  • The function for registering a converter for use in path()routes.

The converter argument is a converter class, and type_name is theconverter name to use in path patterns. SeeRegistering custom path converters for an example.

django.conf.urls functions for use in URLconfs

static()

  • static.static(prefix, view=django.views.static.serve, **kwargs)
  • Helper function to return a URL pattern for serving files in debug mode:
  1. from django.conf import settings
  2. from django.conf.urls.static import static
  3.  
  4. urlpatterns = [
  5. # ... the rest of your URLconf goes here ...
  6. ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

url()

handler400

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

By default, this is django.views.defaults.bad_request(). If youimplement a custom view, be sure it accepts request and exceptionarguments and returns an HttpResponseBadRequest.

handler403

  • handler403
  • A callable, or a string representing the full Python import path to the viewthat should be called if the user doesn't have the permissions required toaccess a resource.

By default, this is django.views.defaults.permission_denied(). If youimplement a custom view, be sure it accepts request and exceptionarguments and returns an HttpResponseForbidden.

handler404

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

By default, this is django.views.defaults.page_not_found(). If youimplement a custom view, be sure it accepts request and exceptionarguments and returns an HttpResponseNotFound.

handler500

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

By default, this is django.views.defaults.server_error(). If youimplement a custom view, be sure it returns anHttpResponseServerError.