{"title":"URL调度器","version":"2.1","locale":"zh-hans","docname":"topics/http/urls","url":"/zh-hans/2.1/topics/http/urls/","canonical":"https://djangodocs.dev/zh-hans/2.1/topics/http/urls/","summary":"对于高质量的Web 应用来说，使用简洁、优雅的URL 模式是一个非常值得重视的细节。Django 允许你自由地设计你的URL，不受框架束缚。 参见万维网的发明者Berners-Lee 的 Cool URIs don't change ，里面有关于为什么URL 应该保持整洁和有意义的卓越论证。 概况 Link to this…","html":"<h1>URL调度器<a class=\"heading-anchor\" href=\"#url-dispatcher\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>对于高质量的Web 应用来说，使用简洁、优雅的URL 模式是一个非常值得重视的细节。Django 允许你自由地设计你的URL，不受框架束缚。</p>\n<p>参见万维网的发明者Berners-Lee 的 <a class=\"reference external\" href=\"https://www.w3.org/Provider/Style/URI\">Cool URIs don't change</a>，里面有关于为什么URL 应该保持整洁和有意义的卓越论证。</p>\n<section id=\"overview\">\n<h2>概况<a class=\"heading-anchor\" href=\"#overview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>为了给一个应用设计URL，你需要创建一个Python 模块，通常被称为**URLconf**(URL configuration)。这个模块是纯粹的Python 代码，包含URL 模式(简单的正则表达式)到Python 函数(你的视图)的简单映射。</p>\n<p>映射可短可长，随便你。它可以引用其它的映射。而且，因为它是纯粹的Python 代码，它可以动态构造。</p>\n<p>Django 还提供根据当前语言翻译URL 的一种方法。更多信息参见 <a class=\"reference internal\" href=\"/zh-hans/2.1/topics/i18n/translation/#url-internationalization\"><span class=\"std std-ref\">国际化文档</span></a>。</p>\n</section>\n<section id=\"how-django-processes-a-request\">\n<span id=\"id1\"></span><h2>Django 如何处理一个请求<a class=\"heading-anchor\" href=\"#how-django-processes-a-request\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>当一个用户请求Django 站点的一个页面，下面是Django 系统决定执行哪个Python 代码使用的算法：</p>\n<ol class=\"arabic simple\">\n<li><p>Django determines the root URLconf module to use. Ordinarily,\nthis is the value of the <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/settings/#std-setting-ROOT_URLCONF\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">ROOT_URLCONF</span></code></a> setting, but if the incoming\n<code class=\"docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code> object has a <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/request-response/#django.http.HttpRequest.urlconf\" title=\"django.http.HttpRequest.urlconf\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">urlconf</span></code></a>\nattribute (set by middleware), its value will be used in place of the\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/settings/#std-setting-ROOT_URLCONF\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">ROOT_URLCONF</span></code></a> setting.</p></li>\n<li><p>Django loads that Python module and looks for the variable\n<code class=\"docutils literal notranslate\"><span class=\"pre\">urlpatterns</span></code>. This should be a Python list of <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.path\" title=\"django.urls.path\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">django.urls.path()</span></code></a>\nand/or <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.re_path\" title=\"django.urls.re_path\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">django.urls.re_path()</span></code></a> instances.</p></li>\n<li><p>Django 依次匹配每个URL 模式，在与请求的URL 匹配的第一个模式停下来。</p></li>\n<li><p>Once one of the URL patterns matches, Django imports and calls the given\nview, which is a simple Python function (or a <a class=\"reference internal\" href=\"/zh-hans/2.1/topics/class-based-views/\"><span class=\"doc\">class-based view</span></a>). The view gets passed the following\narguments:</p>\n<ul class=\"simple\">\n<li><p>一个 <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> 实例。</p></li>\n<li><p>If the matched URL pattern returned no named groups, then the\nmatches from the regular expression are provided as positional arguments.</p></li>\n<li><p>The keyword arguments are made up of any named parts matched by the\npath expression, overridden by any arguments specified in the optional\n<code class=\"docutils literal notranslate\"><span class=\"pre\">kwargs</span></code> argument to <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.path\" title=\"django.urls.path\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">django.urls.path()</span></code></a> or\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.re_path\" title=\"django.urls.re_path\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">django.urls.re_path()</span></code></a>.</p></li>\n</ul>\n</li>\n<li><p>If no URL pattern matches, or if an exception is raised during any\npoint in this process, Django invokes an appropriate\nerror-handling view. See <a class=\"reference internal\" href=\"#error-handling\">Error handling</a> below.</p></li>\n</ol>\n</section>\n<section id=\"example\">\n<h2>例如<a class=\"heading-anchor\" href=\"#example\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>下面是一个简单的 URLconf:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">path</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;articles/2003/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">special_case_2003</span><span class=\"p\">),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;articles/&lt;int:year&gt;/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">year_archive</span><span class=\"p\">),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;articles/&lt;int:year&gt;/&lt;int:month&gt;/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">month_archive</span><span class=\"p\">),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;articles/&lt;int:year&gt;/&lt;int:month&gt;/&lt;slug:slug&gt;/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">article_detail</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>注意：</p>\n<ul class=\"simple\">\n<li><p>To capture a value from the URL, use angle brackets.</p></li>\n<li><p>Captured values can optionally include a converter type. For example, use\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;int:name&gt;</span></code> to capture an integer parameter. If a converter isn't included,\nany string, excluding a <code class=\"docutils literal notranslate\"><span class=\"pre\">/</span></code> character, is matched.</p></li>\n<li><p>There's no need to add a leading slash, because every URL has that. For\nexample, it's <code class=\"docutils literal notranslate\"><span class=\"pre\">articles</span></code>, not <code class=\"docutils literal notranslate\"><span class=\"pre\">/articles</span></code>.</p></li>\n</ul>\n<p>一些请求的例子：</p>\n<ul class=\"simple\">\n<li><p>A request to <code class=\"docutils literal notranslate\"><span class=\"pre\">/articles/2005/03/</span></code> would match the third entry in the\nlist. Django would call the function\n<code class=\"docutils literal notranslate\"><span class=\"pre\">views.month_archive(request,</span> <span class=\"pre\">year=2005,</span> <span class=\"pre\">month=3)</span></code>.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">/articles/2003/</span></code> would match the first pattern in the list, not the\nsecond one, because the patterns are tested in order, and the first one\nis the first test to pass. Feel free to exploit the ordering to insert\nspecial cases like this. Here, Django would call the function\n<code class=\"docutils literal notranslate\"><span class=\"pre\">views.special_case_2003(request)</span></code></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">/articles/2003</span></code> would not match any of these patterns, because each\npattern requires that the URL end with a slash.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">/articles/2003/03/building-a-django-site/</span></code> would match the final\npattern. Django would call the function\n<code class=\"docutils literal notranslate\"><span class=\"pre\">views.article_detail(request,</span> <span class=\"pre\">year=2003,</span> <span class=\"pre\">month=3,</span> <span class=\"pre\">slug=&quot;building-a-django-site&quot;)</span></code>.</p></li>\n</ul>\n</section>\n<section id=\"path-converters\">\n<h2>Path converters<a class=\"heading-anchor\" href=\"#path-converters\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The following path converters are available by default:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code> - Matches any non-empty string, excluding the path separator, <code class=\"docutils literal notranslate\"><span class=\"pre\">'/'</span></code>.\nThis is the default if a converter isn't included in the expression.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">int</span></code> - Matches zero or any positive integer. Returns an int.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">slug</span></code> - Matches any slug string consisting of ASCII letters or numbers,\nplus the hyphen and underscore characters. For example,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">building-your-1st-django-site</span></code>.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">uuid</span></code> - Matches a formatted UUID. To prevent multiple URLs from mapping to\nthe same page, dashes must be included and letters must be lowercase. For\nexample, <code class=\"docutils literal notranslate\"><span class=\"pre\">075194d3-6885-417e-a8a8-6c931e272f00</span></code>. Returns a\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/uuid.html#uuid.UUID\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">UUID</span></code></a> instance.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">path</span></code> - Matches any non-empty string, including the path separator,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'/'</span></code>. This allows you to match against a complete URL path rather than\njust a segment of a URL path as with <code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code>.</p></li>\n</ul>\n</section>\n<section id=\"registering-custom-path-converters\">\n<span id=\"id2\"></span><h2>Registering custom path converters<a class=\"heading-anchor\" href=\"#registering-custom-path-converters\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>For more complex matching requirements, you can define your own path converters.</p>\n<p>A converter is a class that includes the following:</p>\n<ul class=\"simple\">\n<li><p>A <code class=\"docutils literal notranslate\"><span class=\"pre\">regex</span></code> class attribute, as a string.</p></li>\n<li><p>A <code class=\"docutils literal notranslate\"><span class=\"pre\">to_python(self,</span> <span class=\"pre\">value)</span></code> method, which handles converting the matched\nstring into the type that should be passed to the view function. It should\nraise <code class=\"docutils literal notranslate\"><span class=\"pre\">ValueError</span></code> if it can't convert the given value.</p></li>\n<li><p>A <code class=\"docutils literal notranslate\"><span class=\"pre\">to_url(self,</span> <span class=\"pre\">value)</span></code> method, which handles converting the Python type\ninto a string to be used in the URL.</p></li>\n</ul>\n<p>例如:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">FourDigitYearConverter</span><span class=\"p\">:</span>\n    <span class=\"n\">regex</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;[0-9]</span><span class=\"si\">{4}</span><span class=\"s1\">&#39;</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">to_python</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">value</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"n\">value</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">to_url</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">value</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"s1\">&#39;</span><span class=\"si\">%04d</span><span class=\"s1\">&#39;</span> <span class=\"o\">%</span> <span class=\"n\">value</span>\n</code></pre></div>\n<p>Register custom converter classes in your URLconf using\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.register_converter\" title=\"django.urls.register_converter\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">register_converter()</span></code></a>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">path</span><span class=\"p\">,</span> <span class=\"n\">register_converter</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">converters</span><span class=\"p\">,</span> <span class=\"n\">views</span>\n\n<span class=\"n\">register_converter</span><span class=\"p\">(</span><span class=\"n\">converters</span><span class=\"o\">.</span><span class=\"n\">FourDigitYearConverter</span><span class=\"p\">,</span> <span class=\"s1\">&#39;yyyy&#39;</span><span class=\"p\">)</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;articles/2003/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">special_case_2003</span><span class=\"p\">),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;articles/&lt;yyyy:year&gt;/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">year_archive</span><span class=\"p\">),</span>\n    <span class=\"o\">...</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n</section>\n<section id=\"using-regular-expressions\">\n<h2>Using regular expressions<a class=\"heading-anchor\" href=\"#using-regular-expressions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>If the paths and converters syntax isn't sufficient for defining your URL\npatterns, you can also use regular expressions. To do so, use\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.re_path\" title=\"django.urls.re_path\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">re_path()</span></code></a> instead of <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.path\" title=\"django.urls.path\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">path()</span></code></a>.</p>\n<p>In Python regular expressions, the syntax for named regular expression groups\nis <code class=\"docutils literal notranslate\"><span class=\"pre\">(?P&lt;name&gt;pattern)</span></code>, where <code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code> is the name of the group and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">pattern</span></code> is some pattern to match.</p>\n<p>Here's the example URLconf from earlier, rewritten using regular expressions:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">path</span><span class=\"p\">,</span> <span class=\"n\">re_path</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;articles/2003/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">special_case_2003</span><span class=\"p\">),</span>\n    <span class=\"n\">re_path</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^articles/(?P&lt;year&gt;[0-9]</span><span class=\"si\">{4}</span><span class=\"s1\">)/$&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">year_archive</span><span class=\"p\">),</span>\n    <span class=\"n\">re_path</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^articles/(?P&lt;year&gt;[0-9]</span><span class=\"si\">{4}</span><span class=\"s1\">)/(?P&lt;month&gt;[0-9]</span><span class=\"si\">{2}</span><span class=\"s1\">)/$&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">month_archive</span><span class=\"p\">),</span>\n    <span class=\"n\">re_path</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^articles/(?P&lt;year&gt;[0-9]</span><span class=\"si\">{4}</span><span class=\"s1\">)/(?P&lt;month&gt;[0-9]</span><span class=\"si\">{2}</span><span class=\"s1\">)/(?P&lt;slug&gt;[\\w-]+)/$&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">article_detail</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>This accomplishes roughly the same thing as the previous example, except:</p>\n<ul class=\"simple\">\n<li><p>The exact URLs that will match are slightly more constrained. For example,\nthe year 10000 will no longer match since the year integers are constrained\nto be exactly four digits long.</p></li>\n<li><p>Each captured argument is sent to the view as a string, regardless of what\nsort of match the regular expression makes.</p></li>\n</ul>\n<p>When switching from using <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.path\" title=\"django.urls.path\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">path()</span></code></a> to\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.re_path\" title=\"django.urls.re_path\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">re_path()</span></code></a> or vice versa, it's particularly important to be\naware that the type of the view arguments may change, and so you may need to\nadapt your views.</p>\n<section id=\"using-unnamed-regular-expression-groups\">\n<h3>Using unnamed regular expression groups<a class=\"heading-anchor\" href=\"#using-unnamed-regular-expression-groups\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>As well as the named group syntax, e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">(?P&lt;year&gt;[0-9]{4})</span></code>, you can\nalso use the shorter unnamed group, e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">([0-9]{4})</span></code>.</p>\n<p>This usage isn't particularly recommended as it makes it easier to accidentally\nintroduce errors between the intended meaning of a match and the arguments\nof the view.</p>\n<p>In either case, using only one style within a given regex is recommended. When\nboth styles are mixed, any unnamed groups are ignored and only named groups are\npassed to the view function.</p>\n</section>\n<section id=\"nested-arguments\">\n<h3>Nested arguments<a class=\"heading-anchor\" href=\"#nested-arguments\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Regular expressions allow nested arguments, and Django will resolve them and\npass them to the view. When reversing, Django will try to fill in all outer\ncaptured arguments, ignoring any nested captured arguments. Consider the\nfollowing URL patterns which optionally take a page argument:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">re_path</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">re_path</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^blog/(page-(\\d+)/)?$&#39;</span><span class=\"p\">,</span> <span class=\"n\">blog_articles</span><span class=\"p\">),</span>                  <span class=\"c1\"># bad</span>\n    <span class=\"n\">re_path</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^comments/(?:page-(?P&lt;page_number&gt;\\d+)/)?$&#39;</span><span class=\"p\">,</span> <span class=\"n\">comments</span><span class=\"p\">),</span>  <span class=\"c1\"># good</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Both patterns use nested arguments and will resolve: for example,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">blog/page-2/</span></code> will result in a match to <code class=\"docutils literal notranslate\"><span class=\"pre\">blog_articles</span></code> with two\npositional arguments: <code class=\"docutils literal notranslate\"><span class=\"pre\">page-2/</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">2</span></code>. The second pattern for\n<code class=\"docutils literal notranslate\"><span class=\"pre\">comments</span></code> will match <code class=\"docutils literal notranslate\"><span class=\"pre\">comments/page-2/</span></code> with keyword argument\n<code class=\"docutils literal notranslate\"><span class=\"pre\">page_number</span></code> set to 2. The outer argument in this case is a non-capturing\nargument <code class=\"docutils literal notranslate\"><span class=\"pre\">(?:...)</span></code>.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">blog_articles</span></code> view needs the outermost captured argument to be reversed,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">page-2/</span></code> or no arguments in this case, while <code class=\"docutils literal notranslate\"><span class=\"pre\">comments</span></code> can be reversed\nwith either no arguments or a value for <code class=\"docutils literal notranslate\"><span class=\"pre\">page_number</span></code>.</p>\n<p>Nested captured arguments create a strong coupling between the view arguments\nand the URL as illustrated by <code class=\"docutils literal notranslate\"><span class=\"pre\">blog_articles</span></code>: the view receives part of the\nURL (<code class=\"docutils literal notranslate\"><span class=\"pre\">page-2/</span></code>) instead of only the value the view is interested in. This\ncoupling is even more pronounced when reversing, since to reverse the view we\nneed to pass the piece of URL instead of the page number.</p>\n<p>As a rule of thumb, only capture the values the view needs to work with and\nuse non-capturing arguments when the regular expression needs an argument but\nthe view ignores it.</p>\n</section>\n</section>\n<section id=\"what-the-urlconf-searches-against\">\n<h2>URLconf 在什么上查找<a class=\"heading-anchor\" href=\"#what-the-urlconf-searches-against\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>请求的URL被看做是一个普通的Python 字符串， URLconf在其上查找并匹配。进行匹配时将不包括GET或POST请求方式的参数以及域名。</p>\n<p>例如，  <code class=\"docutils literal notranslate\"><span class=\"pre\">https://www.example.com/myapp/</span></code> 请求中，URLconf 将查找 <code class=\"docutils literal notranslate\"><span class=\"pre\">myapp/</span></code></p>\n<p>在 <code class=\"docutils literal notranslate\"><span class=\"pre\">https://www.example.com/myapp/?page=3</span></code> 请求中，URLconf 仍将查找 <code class=\"docutils literal notranslate\"><span class=\"pre\">myapp/</span></code> 。</p>\n<p>URLconf 不检查使用了哪种请求方法。换句话讲，所有的请求方法 —— 即，对同一个URL的无论是 <code class=\"docutils literal notranslate\"><span class=\"pre\">POST请求</span></code> 、 <code class=\"docutils literal notranslate\"><span class=\"pre\">GET请求</span></code> 、或 <code class=\"docutils literal notranslate\"><span class=\"pre\">HEAD</span></code> 请求方法等等 —— 都将路由到相同的函数。</p>\n</section>\n<section id=\"specifying-defaults-for-view-arguments\">\n<h2>指定视图参数的默认值<a class=\"heading-anchor\" href=\"#specifying-defaults-for-view-arguments\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>有一个方便的小技巧是指定视图参数的默认值。 下面是一个URLconf 和视图的示例：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"c1\"># URLconf</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">path</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;blog/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">page</span><span class=\"p\">),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;blog/page&lt;int:num&gt;/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">page</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n\n<span class=\"c1\"># View (in blog/views.py)</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">page</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">,</span> <span class=\"n\">num</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">):</span>\n    <span class=\"c1\"># Output the appropriate page of blog entries, according to num.</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n<p>In the above example, both URL patterns point to the same view --\n<code class=\"docutils literal notranslate\"><span class=\"pre\">views.page</span></code> -- but the first pattern doesn't capture anything from the\nURL. If the first pattern matches, the <code class=\"docutils literal notranslate\"><span class=\"pre\">page()</span></code> function will use its\ndefault argument for <code class=\"docutils literal notranslate\"><span class=\"pre\">num</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">1</span></code>. If the second pattern matches,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">page()</span></code> will use whatever <code class=\"docutils literal notranslate\"><span class=\"pre\">num</span></code> value was captured.</p>\n</section>\n<section id=\"performance\">\n<h2>性能<a class=\"heading-anchor\" href=\"#performance\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Each regular expression in a <code class=\"docutils literal notranslate\"><span class=\"pre\">urlpatterns</span></code> is compiled the first time it's\naccessed. This makes the system blazingly fast.</p>\n</section>\n<section id=\"syntax-of-the-urlpatterns-variable\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">urlpatterns</span></code> 变量的语法<a class=\"heading-anchor\" href=\"#syntax-of-the-urlpatterns-variable\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">urlpatterns</span></code> should be a Python list of <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.path\" title=\"django.urls.path\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">path()</span></code></a> and/or\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.re_path\" title=\"django.urls.re_path\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">re_path()</span></code></a> instances.</p>\n</section>\n<section id=\"error-handling\">\n<h2>错误处理<a class=\"heading-anchor\" href=\"#error-handling\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>When Django can't find a match for the requested URL, or when an exception is\nraised, Django invokes an error-handling view.</p>\n<p>这些情况发生时使用的视图通过4个变量指定。它们的默认值应该满足大部分项目，但是通过赋值给它们以进一步的自定义也是可以的。</p>\n<p>完整的细节请参见 <a class=\"reference internal\" href=\"/zh-hans/2.1/topics/http/views/#customizing-error-views\"><span class=\"std std-ref\">自定义错误视图</span></a>  。</p>\n<p>这些值得在你的根URLconf 中设置。在其它URLconf 中设置这些变量将不会生效果。</p>\n<p>它们的值必须是可调用的或者是表示视图的Python 完整导入路径的字符串，可以方便地调用它们来处理错误情况。</p>\n<p>这些值是：</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">handler400</span></code> -- 查看 <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.conf.urls.handler400\" title=\"django.conf.urls.handler400\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">django.conf.urls.handler400</span></code></a>.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">handler403</span></code> -- 查看 <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.conf.urls.handler403\" title=\"django.conf.urls.handler403\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">django.conf.urls.handler403</span></code></a>.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">handler404</span></code> -- 查看 <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.conf.urls.handler404\" title=\"django.conf.urls.handler404\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">django.conf.urls.handler404</span></code></a>.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">handler500</span></code> -- 查看 <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.conf.urls.handler500\" title=\"django.conf.urls.handler500\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">django.conf.urls.handler500</span></code></a>.</p></li>\n</ul>\n</section>\n<section id=\"including-other-urlconfs\">\n<span id=\"id3\"></span><h2>包含其它的URLconfs<a class=\"heading-anchor\" href=\"#including-other-urlconfs\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>在任何时候，你的 <code class=\"docutils literal notranslate\"><span class=\"pre\">urlpatterns</span></code> 都可以 &quot;include&quot; 其它URLconf 模块。这实际上将一部分URL 放置于其它URL 下面。</p>\n<p>例如，下面是URLconf  <a class=\"reference external\" href=\"https://www.djangoproject.com/\">Django website</a> 自己的URLconf 中一个片段。它包含许多其它URLconf：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">include</span><span class=\"p\">,</span> <span class=\"n\">path</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"c1\"># ... snip ...</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;community/&#39;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">(</span><span class=\"s1\">&#39;aggregator.urls&#39;</span><span class=\"p\">)),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;contact/&#39;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">(</span><span class=\"s1\">&#39;contact.urls&#39;</span><span class=\"p\">)),</span>\n    <span class=\"c1\"># ... snip ...</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Whenever Django encounters <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.include\" title=\"django.urls.include\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">include()</span></code></a>, it chops off\nwhatever part of the URL matched up to that point and sends the remaining\nstring to the included URLconf for further processing.</p>\n<p>Another possibility is to include additional URL patterns by using a list of\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.path\" title=\"django.urls.path\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">path()</span></code></a> instances. For example, consider this URLconf:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">include</span><span class=\"p\">,</span> <span class=\"n\">path</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">apps.main</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span> <span class=\"k\">as</span> <span class=\"n\">main_views</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">credit</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span> <span class=\"k\">as</span> <span class=\"n\">credit_views</span>\n\n<span class=\"n\">extra_patterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;reports/&#39;</span><span class=\"p\">,</span> <span class=\"n\">credit_views</span><span class=\"o\">.</span><span class=\"n\">report</span><span class=\"p\">),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;reports/&lt;int:id&gt;/&#39;</span><span class=\"p\">,</span> <span class=\"n\">credit_views</span><span class=\"o\">.</span><span class=\"n\">report</span><span class=\"p\">),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;charge/&#39;</span><span class=\"p\">,</span> <span class=\"n\">credit_views</span><span class=\"o\">.</span><span class=\"n\">charge</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">,</span> <span class=\"n\">main_views</span><span class=\"o\">.</span><span class=\"n\">homepage</span><span class=\"p\">),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;help/&#39;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">(</span><span class=\"s1\">&#39;apps.help.urls&#39;</span><span class=\"p\">)),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;credit/&#39;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">(</span><span class=\"n\">extra_patterns</span><span class=\"p\">)),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>在这个例子中， <code class=\"docutils literal notranslate\"><span class=\"pre\">/credit/reports/</span></code>  URL将被 <code class=\"docutils literal notranslate\"><span class=\"pre\">credit.views.report()</span></code> 这个Django 视图处理。</p>\n<p>这种方法可以用来去除URLconf 中的冗余，其中某个模式前缀被重复使用。例如，考虑这个URLconf:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">path</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;&lt;page_slug&gt;-&lt;page_id&gt;/history/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">history</span><span class=\"p\">),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;&lt;page_slug&gt;-&lt;page_id&gt;/edit/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">edit</span><span class=\"p\">),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;&lt;page_slug&gt;-&lt;page_id&gt;/discuss/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">discuss</span><span class=\"p\">),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;&lt;page_slug&gt;-&lt;page_id&gt;/permissions/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">permissions</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>我们可以改进它，通过只声明共同的路径前缀一次并将后面的部分分组:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">include</span><span class=\"p\">,</span> <span class=\"n\">path</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;&lt;page_slug&gt;-&lt;page_id&gt;/&#39;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">([</span>\n        <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;history/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">history</span><span class=\"p\">),</span>\n        <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;edit/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">edit</span><span class=\"p\">),</span>\n        <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;discuss/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">discuss</span><span class=\"p\">),</span>\n        <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;permissions/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">permissions</span><span class=\"p\">),</span>\n    <span class=\"p\">])),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<section id=\"captured-parameters\">\n<h3>捕获的参数<a class=\"heading-anchor\" href=\"#captured-parameters\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>被包含的URLconf 会收到来自父URLconf 捕获的任何参数，所以下面的例子是合法的:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"c1\"># In settings/urls/main.py</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">include</span><span class=\"p\">,</span> <span class=\"n\">path</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;&lt;username&gt;/blog/&#39;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">(</span><span class=\"s1\">&#39;foo.urls.blog&#39;</span><span class=\"p\">)),</span>\n<span class=\"p\">]</span>\n\n<span class=\"c1\"># In foo/urls/blog.py</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">path</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">blog</span><span class=\"o\">.</span><span class=\"n\">index</span><span class=\"p\">),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;archive/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">blog</span><span class=\"o\">.</span><span class=\"n\">archive</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>在上面的例子中，捕获的 <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;username&quot;</span></code> 变量将被如期传递给include()指向的URLconf。</p>\n</section>\n</section>\n<section id=\"passing-extra-options-to-view-functions\">\n<span id=\"views-extra-options\"></span><h2>Passing extra options to view functions<a class=\"heading-anchor\" href=\"#passing-extra-options-to-view-functions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>URLconfs have a hook that lets you pass extra arguments to your view functions,\nas a Python dictionary.</p>\n<p>The <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.path\" title=\"django.urls.path\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">path()</span></code></a> function can take an optional third argument\nwhich should be a dictionary of extra keyword arguments to pass to the view\nfunction.</p>\n<p>例如:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">path</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;blog/&lt;int:year&gt;/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">year_archive</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s1\">&#39;foo&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;bar&#39;</span><span class=\"p\">}),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>In this example, for a request to <code class=\"docutils literal notranslate\"><span class=\"pre\">/blog/2005/</span></code>, Django will call\n<code class=\"docutils literal notranslate\"><span class=\"pre\">views.year_archive(request,</span> <span class=\"pre\">year=2005,</span> <span class=\"pre\">foo='bar')</span></code>.</p>\n<p>This technique is used in the\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/contrib/syndication/\"><span class=\"doc\">syndication framework</span></a> to pass metadata and\noptions to views.</p>\n<aside class=\"admonition-dealing-with-conflicts admonition\">\n<p class=\"admonition-title\">Dealing with conflicts</p>\n<p>It's possible to have a URL pattern which captures named keyword arguments,\nand also passes arguments with the same names in its dictionary of extra\narguments. When this happens, the arguments in the dictionary will be used\ninstead of the arguments captured in the URL.</p>\n</aside>\n<section id=\"passing-extra-options-to-include\">\n<h3>Passing extra options to <code class=\"docutils literal notranslate\"><span class=\"pre\">include()</span></code><a class=\"heading-anchor\" href=\"#passing-extra-options-to-include\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Similarly, you can pass extra options to <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.include\" title=\"django.urls.include\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">include()</span></code></a> and\neach line in the included URLconf will be passed the extra options.</p>\n<p>For example, these two URLconf sets are functionally identical:</p>\n<p>Set one:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"c1\"># main.py</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">include</span><span class=\"p\">,</span> <span class=\"n\">path</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;blog/&#39;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">(</span><span class=\"s1\">&#39;inner&#39;</span><span class=\"p\">),</span> <span class=\"p\">{</span><span class=\"s1\">&#39;blog_id&#39;</span><span class=\"p\">:</span> <span class=\"mi\">3</span><span class=\"p\">}),</span>\n<span class=\"p\">]</span>\n\n<span class=\"c1\"># inner.py</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">path</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">mysite</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;archive/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">archive</span><span class=\"p\">),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;about/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">about</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Set two:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"c1\"># main.py</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">include</span><span class=\"p\">,</span> <span class=\"n\">path</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">mysite</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;blog/&#39;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">(</span><span class=\"s1\">&#39;inner&#39;</span><span class=\"p\">)),</span>\n<span class=\"p\">]</span>\n\n<span class=\"c1\"># inner.py</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">path</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;archive/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">archive</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s1\">&#39;blog_id&#39;</span><span class=\"p\">:</span> <span class=\"mi\">3</span><span class=\"p\">}),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;about/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">about</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s1\">&#39;blog_id&#39;</span><span class=\"p\">:</span> <span class=\"mi\">3</span><span class=\"p\">}),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Note that extra options will <em>always</em> be passed to <em>every</em> line in the included\nURLconf, regardless of whether the line's view actually accepts those options\nas valid. For this reason, this technique is only useful if you're certain that\nevery view in the included URLconf accepts the extra options you're passing.</p>\n</section>\n</section>\n<section id=\"reverse-resolution-of-urls\">\n<h2>Reverse resolution of URLs<a class=\"heading-anchor\" href=\"#reverse-resolution-of-urls\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>A common need when working on a Django project is the possibility to obtain URLs\nin their final forms either for embedding in generated content (views and assets\nURLs, URLs shown to the user, etc.) or for handling of the navigation flow on\nthe server side (redirections, etc.)</p>\n<p>It is strongly desirable to avoid hard-coding these URLs (a laborious,\nnon-scalable and error-prone strategy). Equally dangerous is devising ad-hoc\nmechanisms to generate URLs that are parallel to the design described by the\nURLconf, which can result in the production of URLs that become stale over time.</p>\n<p>In other words, what's needed is a DRY mechanism. Among other advantages it\nwould allow evolution of the URL design without having to go over all the\nproject source code to search and replace outdated URLs.</p>\n<p>The primary piece of information we have available to get a URL is an\nidentification (e.g. the name) of the view in charge of handling it. Other\npieces of information that necessarily must participate in the lookup of the\nright URL are the types (positional, keyword) and values of the view arguments.</p>\n<p>Django provides a solution such that the URL mapper is the only repository of\nthe URL design. You feed it with your URLconf and then it can be used in both\ndirections:</p>\n<ul class=\"simple\">\n<li><p>Starting with a URL requested by the user/browser, it calls the right Django\nview providing any arguments it might need with their values as extracted from\nthe URL.</p></li>\n<li><p>Starting with the identification of the corresponding Django view plus the\nvalues of arguments that would be passed to it, obtain the associated URL.</p></li>\n</ul>\n<p>The first one is the usage we've been discussing in the previous sections. The\nsecond one is what is known as <em>reverse resolution of URLs</em>, <em>reverse URL\nmatching</em>, <em>reverse URL lookup</em>, or simply <em>URL reversing</em>.</p>\n<p>Django provides tools for performing URL reversing that match the different\nlayers where URLs are needed:</p>\n<ul class=\"simple\">\n<li><p>In templates: Using the <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/templates/builtins/#std-templatetag-url\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">url</span></code></a> template tag.</p></li>\n<li><p>In Python code: Using the <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urlresolvers/#django.urls.reverse\" title=\"django.urls.reverse\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">reverse()</span></code></a> function.</p></li>\n<li><p>In higher level code related to handling of URLs of Django model instances:\nThe <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/models/instances/#django.db.models.Model.get_absolute_url\" title=\"django.db.models.Model.get_absolute_url\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">get_absolute_url()</span></code></a> method.</p></li>\n</ul>\n<section id=\"examples\">\n<h3>示例<a class=\"heading-anchor\" href=\"#examples\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Consider again this URLconf entry:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">path</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"c1\">#...</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;articles/&lt;int:year&gt;/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">year_archive</span><span class=\"p\">,</span> <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s1\">&#39;news-year-archive&#39;</span><span class=\"p\">),</span>\n    <span class=\"c1\">#...</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>According to this design, the URL for the archive corresponding to year <em>nnnn</em>\nis <code class=\"docutils literal notranslate\"><span class=\"pre\">/articles/&lt;nnnn&gt;/</span></code>.</p>\n<p>You can obtain these in template code by using:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"p\">&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">url</span> <span class=\"s1\">&#39;news-year-archive&#39;</span> <span class=\"m\">2012</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>2012 Archive<span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;</span>\n<span class=\"c\">{# Or with the year in a template context variable: #}</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">yearvar</span> <span class=\"k\">in</span> <span class=\"nv\">year_list</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">url</span> <span class=\"s1\">&#39;news-year-archive&#39;</span> <span class=\"nv\">yearvar</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">yearvar</span> <span class=\"cp\">}}</span> Archive<span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">ul</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>Or in Python code:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.http</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">HttpResponseRedirect</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">reverse</span>\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">redirect_to_year</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"c1\"># ...</span>\n    <span class=\"n\">year</span> <span class=\"o\">=</span> <span class=\"mi\">2006</span>\n    <span class=\"c1\"># ...</span>\n    <span class=\"k\">return</span> <span class=\"n\">HttpResponseRedirect</span><span class=\"p\">(</span><span class=\"n\">reverse</span><span class=\"p\">(</span><span class=\"s1\">&#39;news-year-archive&#39;</span><span class=\"p\">,</span> <span class=\"n\">args</span><span class=\"o\">=</span><span class=\"p\">(</span><span class=\"n\">year</span><span class=\"p\">,)))</span>\n</code></pre></div>\n<p>If, for some reason, it was decided that the URLs where content for yearly\narticle archives are published at should be changed then you would only need to\nchange the entry in the URLconf.</p>\n<p>In some scenarios where views are of a generic nature, a many-to-one\nrelationship might exist between URLs and views. For these cases the view name\nisn't a good enough identifier for it when comes the time of reversing\nURLs. Read the next section to know about the solution Django provides for this.</p>\n</section>\n</section>\n<section id=\"naming-url-patterns\">\n<span id=\"id4\"></span><h2>Naming URL patterns<a class=\"heading-anchor\" href=\"#naming-url-patterns\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>In order to perform URL reversing, you'll need to use <strong>named URL patterns</strong>\nas done in the examples above. The string used for the URL name can contain any\ncharacters you like. You are not restricted to valid Python names.</p>\n<p>When naming URL patterns, choose names that are unlikely to clash with other\napplications' choice of names. If you call your URL pattern <code class=\"docutils literal notranslate\"><span class=\"pre\">comment</span></code>\nand another application does the same thing, the URL that\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urlresolvers/#django.urls.reverse\" title=\"django.urls.reverse\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">reverse()</span></code></a> finds depends on whichever pattern is last in\nyour project's <code class=\"docutils literal notranslate\"><span class=\"pre\">urlpatterns</span></code> list.</p>\n<p>Putting a prefix on your URL names, perhaps derived from the application\nname (such as <code class=\"docutils literal notranslate\"><span class=\"pre\">myapp-comment</span></code> instead of <code class=\"docutils literal notranslate\"><span class=\"pre\">comment</span></code>), decreases the chance\nof collision.</p>\n<p>You can deliberately choose the <em>same URL name</em> as another application if you\nwant to override a view. For example, a common use case is to override the\n<a class=\"reference internal\" href=\"/zh-hans/2.1/topics/auth/default/#django.contrib.auth.views.LoginView\" title=\"django.contrib.auth.views.LoginView\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">LoginView</span></code></a>. Parts of Django and most\nthird-party apps assume that this view has a URL pattern with the name\n<code class=\"docutils literal notranslate\"><span class=\"pre\">login</span></code>. If you have a custom login view and give its URL the name <code class=\"docutils literal notranslate\"><span class=\"pre\">login</span></code>,\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urlresolvers/#django.urls.reverse\" title=\"django.urls.reverse\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">reverse()</span></code></a> will find your custom view as long as it's in\n<code class=\"docutils literal notranslate\"><span class=\"pre\">urlpatterns</span></code> after <code class=\"docutils literal notranslate\"><span class=\"pre\">django.contrib.auth.urls</span></code> is included (if that's\nincluded at all).</p>\n<p>You may also use the same name for multiple URL patterns if they differ in\ntheir arguments. In addition to the URL name, <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urlresolvers/#django.urls.reverse\" title=\"django.urls.reverse\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">reverse()</span></code></a>\nmatches the number of arguments and the names of the keyword arguments.</p>\n</section>\n<section id=\"url-namespaces\">\n<span id=\"topics-http-defining-url-namespaces\"></span><h2>URL namespaces<a class=\"heading-anchor\" href=\"#url-namespaces\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"introduction\">\n<h3>介绍<a class=\"heading-anchor\" href=\"#introduction\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>URL namespaces allow you to uniquely reverse <a class=\"reference internal\" href=\"#naming-url-patterns\"><span class=\"std std-ref\">named URL patterns</span></a> even if different applications use the same URL names.\nIt's a good practice for third-party apps to always use namespaced URLs (as we\ndid in the tutorial). Similarly, it also allows you to reverse URLs if multiple\ninstances of an application are deployed. In other words, since multiple\ninstances of a single application will share named URLs, namespaces provide a\nway to tell these named URLs apart.</p>\n<p>Django applications that make proper use of URL namespacing can be deployed more\nthan once for a particular site. For example <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/contrib/admin/#module-django.contrib.admin\" title=\"django.contrib.admin: Django's admin site.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.admin</span></code></a> has an\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/contrib/admin/#django.contrib.admin.AdminSite\" title=\"django.contrib.admin.AdminSite\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AdminSite</span></code></a> class which allows you to easily\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/contrib/admin/#multiple-admin-sites\"><span class=\"std std-ref\">deploy more than one instance of the admin</span></a>.\nIn a later example, we'll discuss the idea of deploying the polls application\nfrom the tutorial in two different locations so we can serve the same\nfunctionality to two different audiences (authors and publishers).</p>\n<p>A URL namespace comes in two parts, both of which are strings:</p>\n<dl class=\"simple glossary\">\n<dt id=\"term-application-namespace\">application namespace<a class=\"heading-anchor\" href=\"#term-application-namespace\"><span class=\"visually-hidden\">Link to this term</span><span aria-hidden=\"true\">#</span></a></dt><dd><p>This describes the name of the application that is being deployed. Every\ninstance of a single application will have the same application namespace.\nFor example, Django's admin application has the somewhat predictable\napplication namespace of <code class=\"docutils literal notranslate\"><span class=\"pre\">'admin'</span></code>.</p>\n</dd>\n<dt id=\"term-instance-namespace\">instance namespace<a class=\"heading-anchor\" href=\"#term-instance-namespace\"><span class=\"visually-hidden\">Link to this term</span><span aria-hidden=\"true\">#</span></a></dt><dd><p>This identifies a specific instance of an application. Instance namespaces\nshould be unique across your entire project. However, an instance namespace\ncan be the same as the application namespace. This is used to specify a\ndefault instance of an application. For example, the default Django admin\ninstance has an instance namespace of <code class=\"docutils literal notranslate\"><span class=\"pre\">'admin'</span></code>.</p>\n</dd>\n</dl>\n<p>Namespaced URLs are specified using the <code class=\"docutils literal notranslate\"><span class=\"pre\">':'</span></code> operator. For example, the main\nindex page of the admin application is referenced using <code class=\"docutils literal notranslate\"><span class=\"pre\">'admin:index'</span></code>. This\nindicates a namespace of <code class=\"docutils literal notranslate\"><span class=\"pre\">'admin'</span></code>, and a named URL of <code class=\"docutils literal notranslate\"><span class=\"pre\">'index'</span></code>.</p>\n<p>Namespaces can also be nested. The named URL <code class=\"docutils literal notranslate\"><span class=\"pre\">'sports:polls:index'</span></code> would\nlook for a pattern named <code class=\"docutils literal notranslate\"><span class=\"pre\">'index'</span></code> in the namespace <code class=\"docutils literal notranslate\"><span class=\"pre\">'polls'</span></code> that is itself\ndefined within the top-level namespace <code class=\"docutils literal notranslate\"><span class=\"pre\">'sports'</span></code>.</p>\n</section>\n<section id=\"reversing-namespaced-urls\">\n<span id=\"topics-http-reversing-url-namespaces\"></span><h3>Reversing namespaced URLs<a class=\"heading-anchor\" href=\"#reversing-namespaced-urls\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When given a namespaced URL (e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">'polls:index'</span></code>) to resolve, Django splits\nthe fully qualified name into parts and then tries the following lookup:</p>\n<ol class=\"arabic\">\n<li><p>First, Django looks for a matching <a class=\"reference internal\" href=\"#term-application-namespace\"><span class=\"xref std std-term\">application namespace</span></a> (in this\nexample, <code class=\"docutils literal notranslate\"><span class=\"pre\">'polls'</span></code>). This will yield a list of instances of that\napplication.</p></li>\n<li><p>If there is a current application defined, Django finds and returns the URL\nresolver for that instance. The current application can be specified with\nthe <code class=\"docutils literal notranslate\"><span class=\"pre\">current_app</span></code> argument to the <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urlresolvers/#django.urls.reverse\" title=\"django.urls.reverse\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">reverse()</span></code></a>\nfunction.</p>\n<p>The <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/templates/builtins/#std-templatetag-url\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">url</span></code></a> template tag uses the namespace of the currently resolved\nview as the current application in a\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/templates/api/#django.template.RequestContext\" title=\"django.template.RequestContext\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RequestContext</span></code></a>. You can override this default by\nsetting the current application on the <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/request-response/#django.http.HttpRequest.current_app\" title=\"django.http.HttpRequest.current_app\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">request.current_app</span></code></a> attribute.</p>\n</li>\n<li><p>If there is no current application, Django looks for a default\napplication instance. The default application instance is the instance\nthat has an <a class=\"reference internal\" href=\"#term-instance-namespace\"><span class=\"xref std std-term\">instance namespace</span></a> matching the <a class=\"reference internal\" href=\"#term-application-namespace\"><span class=\"xref std std-term\">application\nnamespace</span></a> (in this example, an instance of <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code> called <code class=\"docutils literal notranslate\"><span class=\"pre\">'polls'</span></code>).</p></li>\n<li><p>If there is no default application instance, Django will pick the last\ndeployed instance of the application, whatever its instance name may be.</p></li>\n<li><p>If the provided namespace doesn't match an <a class=\"reference internal\" href=\"#term-application-namespace\"><span class=\"xref std std-term\">application namespace</span></a> in\nstep 1, Django will attempt a direct lookup of the namespace as an\n<a class=\"reference internal\" href=\"#term-instance-namespace\"><span class=\"xref std std-term\">instance namespace</span></a>.</p></li>\n</ol>\n<p>If there are nested namespaces, these steps are repeated for each part of the\nnamespace until only the view name is unresolved. The view name will then be\nresolved into a URL in the namespace that has been found.</p>\n<section id=\"id5\">\n<h4>例如<a class=\"heading-anchor\" href=\"#id5\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>To show this resolution strategy in action, consider an example of two instances\nof the <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code> application from the tutorial: one called <code class=\"docutils literal notranslate\"><span class=\"pre\">'author-polls'</span></code>\nand one called <code class=\"docutils literal notranslate\"><span class=\"pre\">'publisher-polls'</span></code>. Assume we have enhanced that application\nso that it takes the instance namespace into consideration when creating and\ndisplaying polls.</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\">urls.py</figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">include</span><span class=\"p\">,</span> <span class=\"n\">path</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;author-polls/&#39;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">(</span><span class=\"s1\">&#39;polls.urls&#39;</span><span class=\"p\">,</span> <span class=\"n\">namespace</span><span class=\"o\">=</span><span class=\"s1\">&#39;author-polls&#39;</span><span class=\"p\">)),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;publisher-polls/&#39;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">(</span><span class=\"s1\">&#39;polls.urls&#39;</span><span class=\"p\">,</span> <span class=\"n\">namespace</span><span class=\"o\">=</span><span class=\"s1\">&#39;publisher-polls&#39;</span><span class=\"p\">)),</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\">polls/urls.py</figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">path</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span>\n\n<span class=\"n\">app_name</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;polls&#39;</span>\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">IndexView</span><span class=\"o\">.</span><span class=\"n\">as_view</span><span class=\"p\">(),</span> <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s1\">&#39;index&#39;</span><span class=\"p\">),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;&lt;int:pk&gt;/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">DetailView</span><span class=\"o\">.</span><span class=\"n\">as_view</span><span class=\"p\">(),</span> <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s1\">&#39;detail&#39;</span><span class=\"p\">),</span>\n    <span class=\"o\">...</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<p>Using this setup, the following lookups are possible:</p>\n<ul>\n<li><p>If one of the instances is current - say, if we were rendering the detail page\nin the instance <code class=\"docutils literal notranslate\"><span class=\"pre\">'author-polls'</span></code> - <code class=\"docutils literal notranslate\"><span class=\"pre\">'polls:index'</span></code> will resolve to the\nindex page of the <code class=\"docutils literal notranslate\"><span class=\"pre\">'author-polls'</span></code> instance; i.e. both of the following will\nresult in <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;/author-polls/&quot;</span></code>.</p>\n<p>In the method of a class-based view:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">reverse</span><span class=\"p\">(</span><span class=\"s1\">&#39;polls:index&#39;</span><span class=\"p\">,</span> <span class=\"n\">current_app</span><span class=\"o\">=</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">resolver_match</span><span class=\"o\">.</span><span class=\"n\">namespace</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>and in the template:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">url</span> <span class=\"s1\">&#39;polls:index&#39;</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</li>\n<li><p>If there is no current instance - say, if we were rendering a page\nsomewhere else on the site - <code class=\"docutils literal notranslate\"><span class=\"pre\">'polls:index'</span></code> will resolve to the last\nregistered instance of <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code>. Since there is no default instance\n(instance namespace of <code class=\"docutils literal notranslate\"><span class=\"pre\">'polls'</span></code>), the last instance of <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code> that is\nregistered will be used. This would be <code class=\"docutils literal notranslate\"><span class=\"pre\">'publisher-polls'</span></code> since it's\ndeclared last in the <code class=\"docutils literal notranslate\"><span class=\"pre\">urlpatterns</span></code>.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">'author-polls:index'</span></code> will always resolve to the index page of the instance\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'author-polls'</span></code> (and likewise for <code class=\"docutils literal notranslate\"><span class=\"pre\">'publisher-polls'</span></code>) .</p></li>\n</ul>\n<p>If there were also a default instance - i.e., an instance named <code class=\"docutils literal notranslate\"><span class=\"pre\">'polls'</span></code> -\nthe only change from above would be in the case where there is no current\ninstance (the second item in the list above). In this case <code class=\"docutils literal notranslate\"><span class=\"pre\">'polls:index'</span></code>\nwould resolve to the index page of the default instance instead of the instance\ndeclared last in <code class=\"docutils literal notranslate\"><span class=\"pre\">urlpatterns</span></code>.</p>\n</section>\n</section>\n<section id=\"url-namespaces-and-included-urlconfs\">\n<span id=\"namespaces-and-include\"></span><h3>URL namespaces and included URLconfs<a class=\"heading-anchor\" href=\"#url-namespaces-and-included-urlconfs\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Application namespaces of included URLconfs can be specified in two ways.</p>\n<p>Firstly, you can set an <code class=\"docutils literal notranslate\"><span class=\"pre\">app_name</span></code> attribute in the included URLconf module,\nat the same level as the <code class=\"docutils literal notranslate\"><span class=\"pre\">urlpatterns</span></code> attribute. You have to pass the actual\nmodule, or a string reference to the module, to <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.include\" title=\"django.urls.include\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">include()</span></code></a>,\nnot the list of <code class=\"docutils literal notranslate\"><span class=\"pre\">urlpatterns</span></code> itself.</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\">polls/urls.py</figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">path</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span>\n\n<span class=\"n\">app_name</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;polls&#39;</span>\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">IndexView</span><span class=\"o\">.</span><span class=\"n\">as_view</span><span class=\"p\">(),</span> <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s1\">&#39;index&#39;</span><span class=\"p\">),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;&lt;int:pk&gt;/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">DetailView</span><span class=\"o\">.</span><span class=\"n\">as_view</span><span class=\"p\">(),</span> <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s1\">&#39;detail&#39;</span><span class=\"p\">),</span>\n    <span class=\"o\">...</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\">urls.py</figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">include</span><span class=\"p\">,</span> <span class=\"n\">path</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;polls/&#39;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">(</span><span class=\"s1\">&#39;polls.urls&#39;</span><span class=\"p\">)),</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<p>The URLs defined in <code class=\"docutils literal notranslate\"><span class=\"pre\">polls.urls</span></code> will have an application namespace <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code>.</p>\n<p>Secondly, you can include an object that contains embedded namespace data. If\nyou <code class=\"docutils literal notranslate\"><span class=\"pre\">include()</span></code> a list of <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.path\" title=\"django.urls.path\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">path()</span></code></a> or\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.re_path\" title=\"django.urls.re_path\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">re_path()</span></code></a> instances, the URLs contained in that object\nwill be added to the global namespace. However, you can also <code class=\"docutils literal notranslate\"><span class=\"pre\">include()</span></code> a\n2-tuple containing:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"p\">(</span><span class=\"o\">&lt;</span><span class=\"nb\">list</span> <span class=\"n\">of</span> <span class=\"n\">path</span><span class=\"p\">()</span><span class=\"o\">/</span><span class=\"n\">re_path</span><span class=\"p\">()</span> <span class=\"n\">instances</span><span class=\"o\">&gt;</span><span class=\"p\">,</span> <span class=\"o\">&lt;</span><span class=\"n\">application</span> <span class=\"n\">namespace</span><span class=\"o\">&gt;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>例如:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">include</span><span class=\"p\">,</span> <span class=\"n\">path</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">views</span>\n\n<span class=\"n\">polls_patterns</span> <span class=\"o\">=</span> <span class=\"p\">([</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">IndexView</span><span class=\"o\">.</span><span class=\"n\">as_view</span><span class=\"p\">(),</span> <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s1\">&#39;index&#39;</span><span class=\"p\">),</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;&lt;int:pk&gt;/&#39;</span><span class=\"p\">,</span> <span class=\"n\">views</span><span class=\"o\">.</span><span class=\"n\">DetailView</span><span class=\"o\">.</span><span class=\"n\">as_view</span><span class=\"p\">(),</span> <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s1\">&#39;detail&#39;</span><span class=\"p\">),</span>\n<span class=\"p\">],</span> <span class=\"s1\">&#39;polls&#39;</span><span class=\"p\">)</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">path</span><span class=\"p\">(</span><span class=\"s1\">&#39;polls/&#39;</span><span class=\"p\">,</span> <span class=\"n\">include</span><span class=\"p\">(</span><span class=\"n\">polls_patterns</span><span class=\"p\">)),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>This will include the nominated URL patterns into the given application\nnamespace.</p>\n<p>The instance namespace can be specified using the <code class=\"docutils literal notranslate\"><span class=\"pre\">namespace</span></code> argument to\n<a class=\"reference internal\" href=\"/zh-hans/2.1/ref/urls/#django.urls.include\" title=\"django.urls.include\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">include()</span></code></a>. If the instance namespace is not specified,\nit will default to the included URLconf's application namespace. This means\nit will also be the default instance for that namespace.</p>\n</section>\n</section>","rootId":"url-dispatcher","toc":[{"title":"概况","anchor":"overview","children":[]},{"title":"Django 如何处理一个请求","anchor":"how-django-processes-a-request","children":[]},{"title":"例如","anchor":"example","children":[]},{"title":"Path converters","anchor":"path-converters","children":[]},{"title":"Registering custom path converters","anchor":"registering-custom-path-converters","children":[]},{"title":"Using regular expressions","anchor":"using-regular-expressions","children":[{"title":"Using unnamed regular expression groups","anchor":"using-unnamed-regular-expression-groups","children":[]},{"title":"Nested arguments","anchor":"nested-arguments","children":[]}]},{"title":"URLconf 在什么上查找","anchor":"what-the-urlconf-searches-against","children":[]},{"title":"指定视图参数的默认值","anchor":"specifying-defaults-for-view-arguments","children":[]},{"title":"性能","anchor":"performance","children":[]},{"title":"urlpatterns 变量的语法","anchor":"syntax-of-the-urlpatterns-variable","children":[]},{"title":"错误处理","anchor":"error-handling","children":[]},{"title":"包含其它的URLconfs","anchor":"including-other-urlconfs","children":[{"title":"捕获的参数","anchor":"captured-parameters","children":[]}]},{"title":"Passing extra options to view functions","anchor":"passing-extra-options-to-view-functions","children":[{"title":"Passing extra options to include()","anchor":"passing-extra-options-to-include","children":[]}]},{"title":"Reverse resolution of URLs","anchor":"reverse-resolution-of-urls","children":[{"title":"示例","anchor":"examples","children":[]}]},{"title":"Naming URL patterns","anchor":"naming-url-patterns","children":[]},{"title":"URL namespaces","anchor":"url-namespaces","children":[{"title":"介绍","anchor":"introduction","children":[]},{"title":"Reversing namespaced URLs","anchor":"reversing-namespaced-urls","children":[{"title":"例如","anchor":"id5","children":[]}]},{"title":"URL namespaces and included URLconfs","anchor":"url-namespaces-and-included-urlconfs","children":[]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Using Django","url":"/zh-hans/2.1/topics/"},{"docname":"topics/http/index","title":"处理 HTTP 请求","url":"/zh-hans/2.1/topics/http/"}],"prev":{"docname":"topics/http/index","title":"处理 HTTP 请求","url":"/zh-hans/2.1/topics/http/"},"next":{"docname":"topics/http/views","title":"Writing views","url":"/zh-hans/2.1/topics/http/views/"},"formats":{"html":"/zh-hans/2.1/topics/http/urls/","markdown":"/zh-hans/2.1/topics/http/urls.md","json":"/zh-hans/2.1/topics/http/urls.json"},"source":"https://github.com/django/django/blob/stable/2.1.x/docs/topics/http/urls.txt","official":"https://docs.djangoproject.com/zh-hans/2.1/topics/http/urls/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}