Django参考文档:URLconfs 中使用的 django.urls 函数
创始人
2026-04-30 14:55:41
0

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//", views.bio, name="bio"),
    path("articles//", views.article, name="article-detail"),
    path("articles///", 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 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 ) which limits the characters matched and may also change the type of the variable passed to the view. For example, 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\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

相关内容

热门资讯

玻璃硬盘原理图 玻璃硬盘原理 玻璃硬盘,又称为磁头悬浮硬盘(Magnetic Head Flying Disk,MHFD),是一种...
闲鱼搜索规则与技巧 闲鱼最新特... 在闲鱼这个二手交易平台上,有很多用户都希望能够找到一些特殊的东西,比如一些罕见的收藏品、独特的手工艺...
家里监控最长能保存多少天的记录... 家里监控一般保存多久 随着科技的发展,家庭监控系统已经成为了许多家庭的必备设备,它不仅可以帮助我们...
华为tag有用吗 华为tag-... 华为Tag是华为手机中的一种功能,它可以帮助用户更好地管理自己的手机数据和应用,通过使用华为Tag,...
ps5手柄可用手机快充充电吗 ... PS5手柄,即PlayStation 5的DualSense手柄,是索尼公司为PlayStation...
QQ音乐提示代理模式可能无法正... QQ音乐提示代理模式可能无法正常访问,如上图所示,是怎么回事呢? 这个可能和你的网络设置有关系,首先...
收到微信有提示音怎么去掉 微信... 微信收到信息没有提示音,可能是由多种原因导致的,以下是一些可能的原因及解决方法: 1. 手机静音或...
a100显卡对应的cuda版本 在进行GPU加速的编程中,CUDA是常用的架构和平台,其版本和显卡型号之间存在着一定的对应关系。本篇...
别人打电话听不见我说话怎么回事... 当我们在使用手机时,可能会遇到别人打电话过来听不见声音的情况,这种情况可能是由多种原因导致的,下面我...
苹果手机非通讯录电话打不进来 ... 手机电话打不进来可能有多种原因,以下是一些常见的问题及解决方法: 1. **信号问题**: ...