django.urls functions for use in URLconfs

path()

path(route, view, kwargs=None, name=None)
New in Django 2.0.

返回包含在urlpatterns中的元素。 For example:

from django.urls import include, path

urlpatterns = [
    path('index/', views.index, name='main-view'),
    path('bio/<username>/', views.bio, name='bio'),
    path('articles/<slug:title>/', views.article, name='article-detail'),
    path('articles/<slug:title>/<int:section>/', views.section, name='article-section'),
    path('weblog/', include('blog.urls')),
    ...
]

route参数应该是包含URL模式的字符串或gettext_lazy()(请参阅翻译URL模式)。 该字符串可能包含尖括号(如之前的<username>)用来捕获URL的一部分并作为关键字参数传给视图。 尖括号可以包含转换器规范(如<int:section>中的的int部分)用于限制字符的匹配并能改变传至试图的变量的类型。 例如,<int:section>匹配一个数字字符并将其转成int类型。 See How Django processes a request for more details.

view参数是一个视图函数或基于类的视图的as_view()的结果。 It can also be an django.urls.include().

kwargs参数允许您将其他参数传递给视图函数或方法。 有关示例,请参阅传递额外选项以查看函数

有关为什么name参数有用,请参阅命名URL模式

re_path()

re_path(route, view, kwargs=None, name=None)
New in Django 2.0.

Returns an element for inclusion in urlpatterns. For example:

from django.urls import include, re_path

urlpatterns = [
    re_path(r'^index/$', views.index, name='index'),
    re_path(r'^bio/(?P<username>\w+)/$', views.bio, name='bio'),
    re_path(r'^weblog/', include('blog.urls')),
    ...
]

route参数应该是包含与Python的re兼容的正则表达式的字符串或gettext_lazy()(请参阅翻译URL模式模块。 字符串通常使用原始字符串语法(r''),以便它们可以包含像\d这样的序列,而不需要用另一个反斜杠转义反斜杠。 当有字符匹配时,正则表达式中的捕获的字符组将被传递给视图 - 如果字符组有命名则按变量名传递参数,否则按顺序传递参数。 这些值作为字符串传递,不进行任何类型转换。

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

include()

include(module, namespace=None)[source]
include(pattern_list)
include((pattern_list, app_namespace), namespace=None)

将完整的Python导入路径引入另一个URLconf模块的函数。 可选地,可以指定要被包含的条目的应用程序名称空间实例名称空间

通常,应用程序名称空间应该在被包含的模块中指定。 如果设置了应用程序名称空间,则可以使用名称空间参数来设置不同的实例名称空间。

include()还接受一个返回URL模式的迭代器或一个包含这种迭代器和应用程序命名空间名称的2-tuple作为参数。

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

See Including other URLconfs and URL namespaces and included URLconfs.

Changed in Django 2.0:

在旧版本中,该函数位于django.conf.urls中。 旧的位置仍然适用于向后兼容性。

register_converter()

register_converter(converter, type_name)[source]
New in Django 2.0.

注册用于path() 路由的转换器的功能。

converter参数是一个转换器类,type_name是在路径模式中使用的转换器名称。 举例来说,请参阅注册自定义路径转换器

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:

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()

url(regex, view, kwargs=None, name=None)[source]

This function is an alias to django.urls.re_path(). 它可能会在未来的版本中被弃用。

handler400

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

handler403

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

handler404

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

handler500

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