URLconfs 中使用的 django.urls 函数

path()

path(route, view, kwargs=None, name=None)

返回一个元素,以便包含在 urlpatterns 中。例如:

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("blog/", include("blog.urls")),
    ...,
]

route

The route argument should be a string or gettext_lazy() (see 翻译URL模式) that contains a URL pattern. The string may contain angle brackets (like <username> above) to capture part of the URL and send it as a keyword argument to the view. The angle brackets may include a converter specification (like the int part of <int:section>) which limits the characters matched and may also change the type of the variable passed to the view. For example, <int:section> matches a string of decimal digits and converts the value to an int.

在处理请求时,Django 从 urlpatterns 中的第一个模式开始,依次向下遍历列表,将请求的 URL 与每个模式进行比较,直到找到匹配的模式。更多详细信息请参见 Django 如何处理一个请求

模式不会匹配 GET 和 POST 参数或域名。例如,在请求 https://www.example.com/myapp/ 时,URLconf 会查找 myapp/。在请求 https://www.example.com/myapp/?page=3 时,URLconf 同样会查找 myapp/

view

view 参数是一个视图函数或基于类的视图的 as_view() 结果。它也可以是 django.urls.include()

当 Django 找到匹配的模式时,它会调用指定的视图函数,并将 HttpRequest 对象作为第一个参数,将路由中“捕获”的值作为关键字参数传递。

kwargs

kwargs 参数允许你向视图函数或方法传递附加参数。参见 传递额外选项给视图函数 的例子。

name

为你的 URL 取名能使你在 Django 的任意地方唯一地引用它,尤其是在模板中。这个有用的特性允许你只改一个文件就能全局地修改某个 URL 模式。

关于为什么 name 参数是有用的,请参见 命名 URL 模式

re_path()

re_path(route, view, kwargs=None, name=None)

返回一个元素,以便包含在 urlpatterns 中。例如:

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"^blog/", include("blog.urls")),
    ...,
]

The route argument should be a string or gettext_lazy() (see 翻译URL模式) that contains a regular expression compatible with Python's re module. Strings typically use raw string syntax (r'') so that they can contain sequences like \d without the need to escape the backslash with another backslash. When a match is made, captured groups from the regular expression are passed to the view -- as named arguments if the groups are named, and as positional arguments otherwise. The values are passed as strings, without any type conversion.

When a route ends with $ the whole requested URL, matching against path_info, must match the regular expression pattern (re.fullmatch() is used).

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 模块。可以选择指定 application namespaceinstance namespace,在这两个空间中,条目将被包含进去。

通常,应用程序的命名空间应该由包含的模块指定。如果设置了应用程序命名空间,namespace 参数可以用来设置不同的实例命名空间。

include() 也接受一个返回 URL 模式的迭代函数或一个包含这种迭代函数加上应用程序名称空间的二元元组作为参数。

参数:
  • module -- URLconf 模块(或模块名称)

  • namespace (str) -- 包含的 URL 条目的实例命名空间。

  • pattern_list -- 可迭代的 path() 和/或 re_path() 实例。

  • app_namespace (str) -- 被包含的 URL 条目的应用命名空间

参见 包含其它的URLconfsURL 命名空间和包含的 URLconfs

register_converter()

register_converter(converter, type_name)[source]

The function for registering a converter for use in path() routes.

converter 参数是一个转换器类,type_name 是路径模式中使用的转换器名称。参见 注册自定义的路径转换器 的例子。

URLconfs 中使用的 django.conf.urls 函数

static()

static.static(prefix, view=django.views.static.serve, **kwargs)

用于返回在调试模式下服务文件的 URL 模式的辅助函数:

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)

handler400

handler400

一个可调用对象,或者一个代表视图的完整 Python 导入路径的字符串,如果 HTTP 客户端发送了一个引起错误条件的请求,并且响应的状态码为 400,那么就会调用该视图。

默认情况下,这是 django.views.defaults.bad_request()。如果你实现了自定义视图,请确保它接受 requestexception 参数,并返回一个 HttpResponseBadRequest

handler403

handler403

一个可调用对象,或者一个代表视图的完整 Python 导入路径的字符串,如果用户没有访问资源所需的权限,那么就会调用该视图。

默认情况下,这是 django.views.defaults.permission_denied()。如果你实现了一个自定义视图,请确保它接受 requestexception 参数,并返回一个 HttpResponseForbidden

handler404

handler404

一个可调用对象,或者一个代表视图的完整 Python 导入路径的字符串,如果没有任何 URL 模式匹配,那么就会调用该视图。

默认情况下,这是 django.views.defaults.page_not_found()。如果你实现了自定义视图,请确保它接受 requestexception 参数,并返回一个 HttpResponseNotFound

handler500

handler500

一个可调用对象,或者一个代表视图的完整 Python 导入路径的字符串,在服务器出错时会被调用。当你在视图代码中出现运行时错误时,就会发生服务器错误。

默认情况下,这是 django.views.defaults.server_error()。如果你实现了自定义视图,请确保它接受一个 request 参数,并返回一个 HttpResponseServerError