Sophie

Sophie

distrib > Mageia > 7 > x86_64 > by-pkgid > 481c2de1450e70fa8fdc1e3abf72606b > files > 1094

python-django-doc-1.11.20-1.mga7.noarch.rpm


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>URL dispatcher &#8212; Django 1.11.20 documentation</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Writing views" href="views.html" />
    <link rel="prev" title="Handling HTTP requests" href="index.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.11.20 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="index.html" title="Handling HTTP requests">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="views.html" title="Writing views">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-http-urls">
            
  <div class="section" id="s-url-dispatcher">
<span id="url-dispatcher"></span><h1>URL dispatcher<a class="headerlink" href="#url-dispatcher" title="Permalink to this headline">¶</a></h1>
<p>A clean, elegant URL scheme is an important detail in a high-quality Web
application. Django lets you design URLs however you want, with no framework
limitations.</p>
<p>There’s no <code class="docutils literal notranslate"><span class="pre">.php</span></code> or <code class="docutils literal notranslate"><span class="pre">.cgi</span></code> required, and certainly none of that
<code class="docutils literal notranslate"><span class="pre">0,2097,1-1-1928,00</span></code> nonsense.</p>
<p>See <a class="reference external" href="http://www.w3.org/Provider/Style/URI">Cool URIs don’t change</a>, by World Wide Web creator Tim Berners-Lee, for
excellent arguments on why URLs should be clean and usable.</p>
<div class="section" id="s-overview">
<span id="overview"></span><h2>Overview<a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h2>
<p>To design URLs for an app, you create a Python module informally called a
<strong>URLconf</strong> (URL configuration). This module is pure Python code and is a
simple mapping between URL patterns (simple regular expressions) to Python
functions (your views).</p>
<p>This mapping can be as short or as long as needed. It can reference other
mappings. And, because it’s pure Python code, it can be constructed
dynamically.</p>
<p>Django also provides a way to translate URLs according to the active
language. See the <a class="reference internal" href="../i18n/translation.html#url-internationalization"><span class="std std-ref">internationalization documentation</span></a> for more information.</p>
</div>
<div class="section" id="s-how-django-processes-a-request">
<span id="s-id1"></span><span id="how-django-processes-a-request"></span><span id="id1"></span><h2>How Django processes a request<a class="headerlink" href="#how-django-processes-a-request" title="Permalink to this headline">¶</a></h2>
<p>When a user requests a page from your Django-powered site, this is the
algorithm the system follows to determine which Python code to execute:</p>
<ol class="arabic simple">
<li>Django determines the root URLconf module to use. Ordinarily,
this is the value of the <a class="reference internal" href="../../ref/settings.html#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
<code class="docutils literal notranslate"><span class="pre">HttpRequest</span></code> object has a <a class="reference internal" href="../../ref/request-response.html#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>
attribute (set by middleware), its value will be used in place of the
<a class="reference internal" href="../../ref/settings.html#std:setting-ROOT_URLCONF"><code class="xref std std-setting docutils literal notranslate"><span class="pre">ROOT_URLCONF</span></code></a> setting.</li>
<li>Django loads that Python module and looks for the variable
<code class="docutils literal notranslate"><span class="pre">urlpatterns</span></code>. This should be a Python list of <a class="reference internal" href="../../ref/urls.html#django.conf.urls.url" title="django.conf.urls.url"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.conf.urls.url()</span></code></a>
instances.</li>
<li>Django runs through each URL pattern, in order, and stops at the first
one that matches the requested URL.</li>
<li>Once one of the regexes matches, Django imports and calls the given view,
which is a simple Python function (or a <a class="reference internal" href="../class-based-views/index.html"><span class="doc">class-based view</span></a>). The view gets passed the following
arguments:<ul>
<li>An instance of <a class="reference internal" href="../../ref/request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpRequest</span></code></a>.</li>
<li>If the matched regular expression returned no named groups, then the
matches from the regular expression are provided as positional arguments.</li>
<li>The keyword arguments are made up of any named groups matched by the
regular expression, overridden by any arguments specified in the optional
<code class="docutils literal notranslate"><span class="pre">kwargs</span></code> argument to <a class="reference internal" href="../../ref/urls.html#django.conf.urls.url" title="django.conf.urls.url"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.conf.urls.url()</span></code></a>.</li>
</ul>
</li>
<li>If no regex matches, or if an exception is raised during any
point in this process, Django invokes an appropriate
error-handling view. See <a class="reference internal" href="#error-handling">Error handling</a> below.</li>
</ol>
</div>
<div class="section" id="s-example">
<span id="example"></span><h2>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2>
<p>Here’s a sample URLconf:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>

<span class="kn">from</span> <span class="nn">.</span> <span class="k">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^articles/([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>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^articles/([0-9]</span><span class="si">{4}</span><span class="s1">)/([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>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^articles/([0-9]</span><span class="si">{4}</span><span class="s1">)/([0-9]</span><span class="si">{2}</span><span class="s1">)/([0-9]+)/$&#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>
<span class="p">]</span>
</pre></div>
</div>
<p>Notes:</p>
<ul class="simple">
<li>To capture a value from the URL, just put parenthesis around it.</li>
<li>There’s no need to add a leading slash, because every URL has that. For
example, 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>.</li>
<li>The <code class="docutils literal notranslate"><span class="pre">'r'</span></code> in front of each regular expression string is optional but
recommended. It tells Python that a string is “raw” – that nothing in
the string should be escaped. See <a class="reference external" href="http://www.diveintopython3.net/regular-expressions.html#streetaddresses">Dive Into Python’s explanation</a>.</li>
</ul>
<p>Example requests:</p>
<ul class="simple">
<li>A request to <code class="docutils literal notranslate"><span class="pre">/articles/2005/03/</span></code> would match the third entry in the
list. Django would call the function
<code class="docutils literal notranslate"><span class="pre">views.month_archive(request,</span> <span class="pre">'2005',</span> <span class="pre">'03')</span></code>.</li>
<li><code class="docutils literal notranslate"><span class="pre">/articles/2005/3/</span></code> would not match any URL patterns, because the
third entry in the list requires two digits for the month.</li>
<li><code class="docutils literal notranslate"><span class="pre">/articles/2003/</span></code> would match the first pattern in the list, not the
second one, because the patterns are tested in order, and the first one
is the first test to pass. Feel free to exploit the ordering to insert
special cases like this. Here, Django would call the function
<code class="docutils literal notranslate"><span class="pre">views.special_case_2003(request)</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">/articles/2003</span></code> would not match any of these patterns, because each
pattern requires that the URL end with a slash.</li>
<li><code class="docutils literal notranslate"><span class="pre">/articles/2003/03/03/</span></code> would match the final pattern. Django would call
the function <code class="docutils literal notranslate"><span class="pre">views.article_detail(request,</span> <span class="pre">'2003',</span> <span class="pre">'03',</span> <span class="pre">'03')</span></code>.</li>
</ul>
</div>
<div class="section" id="s-named-groups">
<span id="named-groups"></span><h2>Named groups<a class="headerlink" href="#named-groups" title="Permalink to this headline">¶</a></h2>
<p>The above example used simple, <em>non-named</em> regular-expression groups (via
parenthesis) to capture bits of the URL and pass them as <em>positional</em> arguments
to a view. In more advanced usage, it’s possible to use <em>named</em>
regular-expression groups to capture URL bits and pass them as <em>keyword</em>
arguments to a view.</p>
<p>In Python regular expressions, the syntax for named regular-expression groups
is <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
<code class="docutils literal notranslate"><span class="pre">pattern</span></code> is some pattern to match.</p>
<p>Here’s the above example URLconf, rewritten to use named groups:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>

<span class="kn">from</span> <span class="nn">.</span> <span class="k">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
    <span class="n">url</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>
    <span class="n">url</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>
    <span class="n">url</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;day&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">article_detail</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>This accomplishes exactly the same thing as the previous example, with one
subtle difference: The captured values are passed to view functions as keyword
arguments rather than positional arguments. For example:</p>
<ul class="simple">
<li>A request to <code class="docutils literal notranslate"><span class="pre">/articles/2005/03/</span></code> would call the function
<code class="docutils literal notranslate"><span class="pre">views.month_archive(request,</span> <span class="pre">year='2005',</span> <span class="pre">month='03')</span></code>, instead
of <code class="docutils literal notranslate"><span class="pre">views.month_archive(request,</span> <span class="pre">'2005',</span> <span class="pre">'03')</span></code>.</li>
<li>A request to <code class="docutils literal notranslate"><span class="pre">/articles/2003/03/03/</span></code> would call the function
<code class="docutils literal notranslate"><span class="pre">views.article_detail(request,</span> <span class="pre">year='2003',</span> <span class="pre">month='03',</span> <span class="pre">day='03')</span></code>.</li>
</ul>
<p>In practice, this means your URLconfs are slightly more explicit and less prone
to argument-order bugs – and you can reorder the arguments in your views’
function definitions. Of course, these benefits come at the cost of brevity;
some developers find the named-group syntax ugly and too verbose.</p>
<div class="section" id="s-the-matching-grouping-algorithm">
<span id="the-matching-grouping-algorithm"></span><h3>The matching/grouping algorithm<a class="headerlink" href="#the-matching-grouping-algorithm" title="Permalink to this headline">¶</a></h3>
<p>Here’s the algorithm the URLconf parser follows, with respect to named groups
vs. non-named groups in a regular expression:</p>
<ol class="arabic simple">
<li>If there are any named arguments, it will use those, ignoring non-named
arguments.</li>
<li>Otherwise, it will pass all non-named arguments as positional arguments.</li>
</ol>
<p>In both cases, any extra keyword arguments that have been given as per <a class="reference internal" href="#passing-extra-options-to-view-functions">Passing
extra options to view functions</a> (below) will also be passed to the view.</p>
</div>
</div>
<div class="section" id="s-what-the-urlconf-searches-against">
<span id="what-the-urlconf-searches-against"></span><h2>What the URLconf searches against<a class="headerlink" href="#what-the-urlconf-searches-against" title="Permalink to this headline">¶</a></h2>
<p>The URLconf searches against the requested URL, as a normal Python string. This
does not include GET or POST parameters, or the domain name.</p>
<p>For example, in a request to <code class="docutils literal notranslate"><span class="pre">https://www.example.com/myapp/</span></code>, the URLconf
will look for <code class="docutils literal notranslate"><span class="pre">myapp/</span></code>.</p>
<p>In a request to <code class="docutils literal notranslate"><span class="pre">https://www.example.com/myapp/?page=3</span></code>, the URLconf will look
for <code class="docutils literal notranslate"><span class="pre">myapp/</span></code>.</p>
<p>The URLconf doesn’t look at the request method. In other words, all request
methods – <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>, etc. – will be routed to the same
function for the same URL.</p>
</div>
<div class="section" id="s-captured-arguments-are-always-strings">
<span id="captured-arguments-are-always-strings"></span><h2>Captured arguments are always strings<a class="headerlink" href="#captured-arguments-are-always-strings" title="Permalink to this headline">¶</a></h2>
<p>Each captured argument is sent to the view as a plain Python string, regardless
of what sort of match the regular expression makes. For example, in this
URLconf line:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">url</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>
</pre></div>
</div>
<dl class="docutils">
<dt>…the <code class="docutils literal notranslate"><span class="pre">year</span></code> argument passed to <code class="docutils literal notranslate"><span class="pre">views.year_archive()</span></code> will be a string,</dt>
<dd>not an integer, even though the <code class="docutils literal notranslate"><span class="pre">[0-9]{4}</span></code> will only match integer strings.</dd>
</dl>
</div>
<div class="section" id="s-specifying-defaults-for-view-arguments">
<span id="specifying-defaults-for-view-arguments"></span><h2>Specifying defaults for view arguments<a class="headerlink" href="#specifying-defaults-for-view-arguments" title="Permalink to this headline">¶</a></h2>
<p>A convenient trick is to specify default parameters for your views’ arguments.
Here’s an example URLconf and view:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># URLconf</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>

<span class="kn">from</span> <span class="nn">.</span> <span class="k">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^blog/page(?P&lt;num&gt;[0-9]+)/$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">page</span><span class="p">),</span>
<span class="p">]</span>

<span class="c1"># View (in blog/views.py)</span>
<span class="k">def</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="s2">&quot;1&quot;</span><span class="p">):</span>
    <span class="c1"># Output the appropriate page of blog entries, according to num.</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>In the above example, both URL patterns point to the same view –
<code class="docutils literal notranslate"><span class="pre">views.page</span></code> – but the first pattern doesn’t capture anything from the
URL. If the first pattern matches, the <code class="docutils literal notranslate"><span class="pre">page()</span></code> function will use its
default argument for <code class="docutils literal notranslate"><span class="pre">num</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;1&quot;</span></code>. If the second pattern matches,
<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 by the regex.</p>
</div>
<div class="section" id="s-performance">
<span id="performance"></span><h2>Performance<a class="headerlink" href="#performance" title="Permalink to this headline">¶</a></h2>
<p>Each regular expression in a <code class="docutils literal notranslate"><span class="pre">urlpatterns</span></code> is compiled the first time it’s
accessed. This makes the system blazingly fast.</p>
</div>
<div class="section" id="s-syntax-of-the-urlpatterns-variable">
<span id="syntax-of-the-urlpatterns-variable"></span><h2>Syntax of the <code class="docutils literal notranslate"><span class="pre">urlpatterns</span></code> variable<a class="headerlink" href="#syntax-of-the-urlpatterns-variable" title="Permalink to this headline">¶</a></h2>
<p><code class="docutils literal notranslate"><span class="pre">urlpatterns</span></code> should be a Python list of <a class="reference internal" href="../../ref/urls.html#django.conf.urls.url" title="django.conf.urls.url"><code class="xref py py-func docutils literal notranslate"><span class="pre">url()</span></code></a>
instances.</p>
</div>
<div class="section" id="s-error-handling">
<span id="error-handling"></span><h2>Error handling<a class="headerlink" href="#error-handling" title="Permalink to this headline">¶</a></h2>
<p>When Django can’t find a regex matching the requested URL, or when an
exception is raised, Django will invoke an error-handling view.</p>
<p>The views to use for these cases are specified by four variables. Their
default values should suffice for most projects, but further customization is
possible by overriding their default values.</p>
<p>See the documentation on <a class="reference internal" href="views.html#customizing-error-views"><span class="std std-ref">customizing error views</span></a> for the full details.</p>
<p>Such values can be set in your root URLconf. Setting these variables in any
other URLconf will have no effect.</p>
<p>Values must be callables, or strings representing the full Python import path
to the view that should be called to handle the error condition at hand.</p>
<p>The variables are:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">handler400</span></code> – See <a class="reference internal" href="../../ref/urls.html#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>.</li>
<li><code class="docutils literal notranslate"><span class="pre">handler403</span></code> – See <a class="reference internal" href="../../ref/urls.html#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>.</li>
<li><code class="docutils literal notranslate"><span class="pre">handler404</span></code> – See <a class="reference internal" href="../../ref/urls.html#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>.</li>
<li><code class="docutils literal notranslate"><span class="pre">handler500</span></code> – See <a class="reference internal" href="../../ref/urls.html#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>.</li>
</ul>
</div>
<div class="section" id="s-including-other-urlconfs">
<span id="s-id2"></span><span id="including-other-urlconfs"></span><span id="id2"></span><h2>Including other URLconfs<a class="headerlink" href="#including-other-urlconfs" title="Permalink to this headline">¶</a></h2>
<p>At any point, your <code class="docutils literal notranslate"><span class="pre">urlpatterns</span></code> can “include” other URLconf modules. This
essentially “roots” a set of URLs below other ones.</p>
<p>For example, here’s an excerpt of the URLconf for the <a class="reference external" href="https://www.djangoproject.com/">Django website</a>
itself. It includes a number of other URLconfs:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="c1"># ... snip ...</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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;django_website.aggregator.urls&#39;</span><span class="p">)),</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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;django_website.contact.urls&#39;</span><span class="p">)),</span>
    <span class="c1"># ... snip ...</span>
<span class="p">]</span>
</pre></div>
</div>
<p>Note that the regular expressions in this example don’t have a <code class="docutils literal notranslate"><span class="pre">$</span></code>
(end-of-string match character) but do include a trailing slash. Whenever
Django encounters <code class="docutils literal notranslate"><span class="pre">include()</span></code> (<a class="reference internal" href="../../ref/urls.html#django.conf.urls.include" title="django.conf.urls.include"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.conf.urls.include()</span></code></a>), it chops
off whatever part of the URL matched up to that point and sends the remaining
string to the included URLconf for further processing.</p>
<p>Another possibility is to include additional URL patterns by using a list of
<a class="reference internal" href="../../ref/urls.html#django.conf.urls.url" title="django.conf.urls.url"><code class="xref py py-func docutils literal notranslate"><span class="pre">url()</span></code></a> instances. For example, consider this URLconf:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">url</span>

<span class="kn">from</span> <span class="nn">apps.main</span> <span class="k">import</span> <span class="n">views</span> <span class="k">as</span> <span class="n">main_views</span>
<span class="kn">from</span> <span class="nn">credit</span> <span class="k">import</span> <span class="n">views</span> <span class="k">as</span> <span class="n">credit_views</span>

<span class="n">extra_patterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^reports/(?P&lt;id&gt;[0-9]+)/$&#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>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
<span class="p">]</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
<span class="p">]</span>
</pre></div>
</div>
<p>In this example, the <code class="docutils literal notranslate"><span class="pre">/credit/reports/</span></code> URL will be handled by the
<code class="docutils literal notranslate"><span class="pre">credit_views.report()</span></code> Django view.</p>
<p>This can be used to remove redundancy from URLconfs where a single pattern
prefix is used repeatedly. For example, consider this URLconf:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">.</span> <span class="k">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^(?P&lt;page_slug&gt;[\w-]+)-(?P&lt;page_id&gt;\w+)/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>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^(?P&lt;page_slug&gt;[\w-]+)-(?P&lt;page_id&gt;\w+)/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>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^(?P&lt;page_slug&gt;[\w-]+)-(?P&lt;page_id&gt;\w+)/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>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^(?P&lt;page_slug&gt;[\w-]+)-(?P&lt;page_id&gt;\w+)/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>
<span class="p">]</span>
</pre></div>
</div>
<p>We can improve this by stating the common path prefix only once and grouping
the suffixes that differ:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">.</span> <span class="k">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^(?P&lt;page_slug&gt;[\w-]+)-(?P&lt;page_id&gt;\w+)/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">([</span>
        <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
        <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
        <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
        <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
    <span class="p">])),</span>
<span class="p">]</span>
</pre></div>
</div>
<div class="section" id="s-captured-parameters">
<span id="captured-parameters"></span><h3>Captured parameters<a class="headerlink" href="#captured-parameters" title="Permalink to this headline">¶</a></h3>
<p>An included URLconf receives any captured parameters from parent URLconfs, so
the following example is valid:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># In settings/urls/main.py</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^(?P&lt;username&gt;\w+)/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>
<span class="p">]</span>

<span class="c1"># In foo/urls/blog.py</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">.</span> <span class="k">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
<span class="p">]</span>
</pre></div>
</div>
<p>In the above example, the captured <code class="docutils literal notranslate"><span class="pre">&quot;username&quot;</span></code> variable is passed to the
included URLconf, as expected.</p>
</div>
</div>
<div class="section" id="s-nested-arguments">
<span id="nested-arguments"></span><h2>Nested arguments<a class="headerlink" href="#nested-arguments" title="Permalink to this headline">¶</a></h2>
<p>Regular expressions allow nested arguments, and Django will resolve them and
pass them to the view. When reversing, Django will try to fill in all outer
captured arguments, ignoring any nested captured arguments. Consider the
following URL patterns which optionally take a page argument:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</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>
    <span class="n">url</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>
<span class="p">]</span>
</pre></div>
</div>
<p>Both patterns use nested arguments and will resolve: for example,
<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
positional 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
<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
<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
argument <code class="docutils literal notranslate"><span class="pre">(?:...)</span></code>.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">blog_articles</span></code> view needs the outermost captured argument to be reversed,
<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
with either no arguments or a value for <code class="docutils literal notranslate"><span class="pre">page_number</span></code>.</p>
<p>Nested captured arguments create a strong coupling between the view arguments
and the URL as illustrated by <code class="docutils literal notranslate"><span class="pre">blog_articles</span></code>: the view receives part of the
URL (<code class="docutils literal notranslate"><span class="pre">page-2/</span></code>) instead of only the value the view is interested in. This
coupling is even more pronounced when reversing, since to reverse the view we
need to pass the piece of URL instead of the page number.</p>
<p>As a rule of thumb, only capture the values the view needs to work with and
use non-capturing arguments when the regular expression needs an argument but
the view ignores it.</p>
</div>
<div class="section" id="s-passing-extra-options-to-view-functions">
<span id="s-views-extra-options"></span><span id="passing-extra-options-to-view-functions"></span><span id="views-extra-options"></span><h2>Passing extra options to view functions<a class="headerlink" href="#passing-extra-options-to-view-functions" title="Permalink to this headline">¶</a></h2>
<p>URLconfs have a hook that lets you pass extra arguments to your view functions,
as a Python dictionary.</p>
<p>The <a class="reference internal" href="../../ref/urls.html#django.conf.urls.url" title="django.conf.urls.url"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.conf.urls.url()</span></code></a> function can take an optional third argument
which should be a dictionary of extra keyword arguments to pass to the view
function.</p>
<p>For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">.</span> <span class="k">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^blog/(?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> <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>
<span class="p">]</span>
</pre></div>
</div>
<p>In this example, for a request to <code class="docutils literal notranslate"><span class="pre">/blog/2005/</span></code>, Django will call
<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>
<p>This technique is used in the
<a class="reference internal" href="../../ref/contrib/syndication.html"><span class="doc">syndication framework</span></a> to pass metadata and
options to views.</p>
<div class="admonition-dealing-with-conflicts admonition">
<p class="first admonition-title">Dealing with conflicts</p>
<p class="last">It’s possible to have a URL pattern which captures named keyword arguments,
and also passes arguments with the same names in its dictionary of extra
arguments. When this happens, the arguments in the dictionary will be used
instead of the arguments captured in the URL.</p>
</div>
<div class="section" id="s-passing-extra-options-to-include">
<span id="passing-extra-options-to-include"></span><h3>Passing extra options to <code class="docutils literal notranslate"><span class="pre">include()</span></code><a class="headerlink" href="#passing-extra-options-to-include" title="Permalink to this headline">¶</a></h3>
<p>Similarly, you can pass extra options to <a class="reference internal" href="../../ref/urls.html#django.conf.urls.include" title="django.conf.urls.include"><code class="xref py py-func docutils literal notranslate"><span class="pre">include()</span></code></a>.
When you pass extra options to <code class="docutils literal notranslate"><span class="pre">include()</span></code>, <em>each</em> line in the included
URLconf will be passed the extra options.</p>
<p>For example, these two URLconf sets are functionally identical:</p>
<p>Set one:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># main.py</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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;blogid&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">}),</span>
<span class="p">]</span>

<span class="c1"># inner.py</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">mysite</span> <span class="k">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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="n">url</span><span class="p">(</span><span class="sa">r</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>
</pre></div>
</div>
<p>Set two:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># main.py</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">url</span>
<span class="kn">from</span> <span class="nn">mysite</span> <span class="k">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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="c1"># inner.py</span>
<span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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;blogid&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">}),</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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;blogid&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">}),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>Note that extra options will <em>always</em> be passed to <em>every</em> line in the included
URLconf, regardless of whether the line’s view actually accepts those options
as valid. For this reason, this technique is only useful if you’re certain that
every view in the included URLconf accepts the extra options you’re passing.</p>
</div>
</div>
<div class="section" id="s-reverse-resolution-of-urls">
<span id="reverse-resolution-of-urls"></span><h2>Reverse resolution of URLs<a class="headerlink" href="#reverse-resolution-of-urls" title="Permalink to this headline">¶</a></h2>
<p>A common need when working on a Django project is the possibility to obtain URLs
in their final forms either for embedding in generated content (views and assets
URLs, URLs shown to the user, etc.) or for handling of the navigation flow on
the server side (redirections, etc.)</p>
<p>It is strongly desirable to avoid hard-coding these URLs (a laborious,
non-scalable and error-prone strategy). Equally dangerous is devising ad-hoc
mechanisms to generate URLs that are parallel to the design described by the
URLconf, which can result in the production of URLs that become stale over time.</p>
<p>In other words, what’s needed is a DRY mechanism. Among other advantages it
would allow evolution of the URL design without having to go over all the
project source code to search and replace outdated URLs.</p>
<p>The primary piece of information we have available to get a URL is an
identification (e.g. the name) of the view in charge of handling it. Other
pieces of information that necessarily must participate in the lookup of the
right URL are the types (positional, keyword) and values of the view arguments.</p>
<p>Django provides a solution such that the URL mapper is the only repository of
the URL design. You feed it with your URLconf and then it can be used in both
directions:</p>
<ul class="simple">
<li>Starting with a URL requested by the user/browser, it calls the right Django
view providing any arguments it might need with their values as extracted from
the URL.</li>
<li>Starting with the identification of the corresponding Django view plus the
values of arguments that would be passed to it, obtain the associated URL.</li>
</ul>
<p>The first one is the usage we’ve been discussing in the previous sections. The
second one is what is known as <em>reverse resolution of URLs</em>, <em>reverse URL
matching</em>, <em>reverse URL lookup</em>, or simply <em>URL reversing</em>.</p>
<p>Django provides tools for performing URL reversing that match the different
layers where URLs are needed:</p>
<ul class="simple">
<li>In templates: Using the <a class="reference internal" href="../../ref/templates/builtins.html#std:templatetag-url"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">url</span></code></a> template tag.</li>
<li>In Python code: Using the <a class="reference internal" href="../../ref/urlresolvers.html#django.urls.reverse" title="django.urls.reverse"><code class="xref py py-func docutils literal notranslate"><span class="pre">reverse()</span></code></a> function.</li>
<li>In higher level code related to handling of URLs of Django model instances:
The <a class="reference internal" href="../../ref/models/instances.html#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.</li>
</ul>
<div class="section" id="s-examples">
<span id="examples"></span><h3>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h3>
<p>Consider again this URLconf entry:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>

<span class="kn">from</span> <span class="nn">.</span> <span class="k">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="c1">#...</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^articles/([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> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;news-year-archive&#39;</span><span class="p">),</span>
    <span class="c1">#...</span>
<span class="p">]</span>
</pre></div>
</div>
<p>According to this design, the URL for the archive corresponding to year <em>nnnn</em>
is <code class="docutils literal notranslate"><span class="pre">/articles/nnnn/</span></code>.</p>
<p>You can obtain these in template code by using:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><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>
<span class="c">{# Or with the year in a template context variable: #}</span>
<span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
<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>
<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>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Or in Python code:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.urls</span> <span class="k">import</span> <span class="n">reverse</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponseRedirect</span>

<span class="k">def</span> <span class="nf">redirect_to_year</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="c1"># ...</span>
    <span class="n">year</span> <span class="o">=</span> <span class="mi">2006</span>
    <span class="c1"># ...</span>
    <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>
</pre></div>
</div>
<p>If, for some reason, it was decided that the URLs where content for yearly
article archives are published at should be changed then you would only need to
change the entry in the URLconf.</p>
<p>In some scenarios where views are of a generic nature, a many-to-one
relationship might exist between URLs and views. For these cases the view name
isn’t a good enough identifier for it when comes the time of reversing
URLs. Read the next section to know about the solution Django provides for this.</p>
</div>
</div>
<div class="section" id="s-naming-url-patterns">
<span id="s-id3"></span><span id="naming-url-patterns"></span><span id="id3"></span><h2>Naming URL patterns<a class="headerlink" href="#naming-url-patterns" title="Permalink to this headline">¶</a></h2>
<p>In order to perform URL reversing, you’ll need to use <strong>named URL patterns</strong>
as done in the examples above. The string used for the URL name can contain any
characters you like. You are not restricted to valid Python names.</p>
<p>When naming URL patterns, choose names that are unlikely to clash with other
applications’ choice of names. If you call your URL pattern <code class="docutils literal notranslate"><span class="pre">comment</span></code>
and another application does the same thing, the URL that
<a class="reference internal" href="../../ref/urlresolvers.html#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
your project’s <code class="docutils literal notranslate"><span class="pre">urlpatterns</span></code> list.</p>
<p>Putting a prefix on your URL names, perhaps derived from the application
name (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
of collision.</p>
<p>You can deliberately choose the <em>same URL name</em> as another application if you
want to override a view. For example, a common use case is to override the
<a class="reference internal" href="../auth/default.html#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
third-party apps assume that this view has a URL pattern with the name
<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>,
<a class="reference internal" href="../../ref/urlresolvers.html#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
<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
included at all).</p>
<p>You may also use the same name for multiple URL patterns if they differ in
their arguments. In addition to the URL name, <a class="reference internal" href="../../ref/urlresolvers.html#django.urls.reverse" title="django.urls.reverse"><code class="xref py py-func docutils literal notranslate"><span class="pre">reverse()</span></code></a>
matches the number of arguments and the names of the keyword arguments.</p>
</div>
<div class="section" id="s-url-namespaces">
<span id="s-topics-http-defining-url-namespaces"></span><span id="url-namespaces"></span><span id="topics-http-defining-url-namespaces"></span><h2>URL namespaces<a class="headerlink" href="#url-namespaces" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-introduction">
<span id="introduction"></span><h3>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h3>
<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.
It’s a good practice for third-party apps to always use namespaced URLs (as we
did in the tutorial). Similarly, it also allows you to reverse URLs if multiple
instances of an application are deployed. In other words, since multiple
instances of a single application will share named URLs, namespaces provide a
way to tell these named URLs apart.</p>
<p>Django applications that make proper use of URL namespacing can be deployed more
than once for a particular site. For example <a class="reference internal" href="../../ref/contrib/admin/index.html#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
<a class="reference internal" href="../../ref/contrib/admin/index.html#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
<a class="reference internal" href="../../ref/contrib/admin/index.html#multiple-admin-sites"><span class="std std-ref">deploy more than one instance of the admin</span></a>.
In a later example, we’ll discuss the idea of deploying the polls application
from the tutorial in two different locations so we can serve the same
functionality to two different audiences (authors and publishers).</p>
<p>A URL namespace comes in two parts, both of which are strings:</p>
<dl class="glossary docutils">
<dt id="term-application-namespace">application namespace</dt>
<dd>This describes the name of the application that is being deployed. Every
instance of a single application will have the same application namespace.
For example, Django’s admin application has the somewhat predictable
application namespace of <code class="docutils literal notranslate"><span class="pre">'admin'</span></code>.</dd>
<dt id="term-instance-namespace">instance namespace</dt>
<dd>This identifies a specific instance of an application. Instance namespaces
should be unique across your entire project. However, an instance namespace
can be the same as the application namespace. This is used to specify a
default instance of an application. For example, the default Django admin
instance has an instance namespace of <code class="docutils literal notranslate"><span class="pre">'admin'</span></code>.</dd>
</dl>
<p>Namespaced URLs are specified using the <code class="docutils literal notranslate"><span class="pre">':'</span></code> operator. For example, the main
index page of the admin application is referenced using <code class="docutils literal notranslate"><span class="pre">'admin:index'</span></code>. This
indicates 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>
<p>Namespaces can also be nested. The named URL <code class="docutils literal notranslate"><span class="pre">'sports:polls:index'</span></code> would
look 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
defined within the top-level namespace <code class="docutils literal notranslate"><span class="pre">'sports'</span></code>.</p>
</div>
<div class="section" id="s-reversing-namespaced-urls">
<span id="s-topics-http-reversing-url-namespaces"></span><span id="reversing-namespaced-urls"></span><span id="topics-http-reversing-url-namespaces"></span><h3>Reversing namespaced URLs<a class="headerlink" href="#reversing-namespaced-urls" title="Permalink to this headline">¶</a></h3>
<p>When given a namespaced URL (e.g. <code class="docutils literal notranslate"><span class="pre">'polls:index'</span></code>) to resolve, Django splits
the fully qualified name into parts and then tries the following lookup:</p>
<ol class="arabic">
<li><p class="first">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
example, <code class="docutils literal notranslate"><span class="pre">'polls'</span></code>). This will yield a list of instances of that
application.</p>
</li>
<li><p class="first">If there is a current application defined, Django finds and returns the URL
resolver for that instance. The current application can be specified with
the <code class="docutils literal notranslate"><span class="pre">current_app</span></code> argument to the <a class="reference internal" href="../../ref/urlresolvers.html#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>
<p>The <a class="reference internal" href="../../ref/templates/builtins.html#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
view as the current application in a
<a class="reference internal" href="../../ref/templates/api.html#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
setting the current application on the <a class="reference internal" href="../../ref/request-response.html#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>
</li>
<li><p class="first">If there is no current application. Django looks for a default
application instance. The default application instance is the instance
that 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
namespace</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>
<li><p class="first">If there is no default application instance, Django will pick the last
deployed instance of the application, whatever its instance name may be.</p>
</li>
<li><p class="first">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
step 1, Django will attempt a direct lookup of the namespace as an
<a class="reference internal" href="#term-instance-namespace"><span class="xref std std-term">instance namespace</span></a>.</p>
</li>
</ol>
<p>If there are nested namespaces, these steps are repeated for each part of the
namespace until only the view name is unresolved. The view name will then be
resolved into a URL in the namespace that has been found.</p>
<div class="section" id="s-id4">
<span id="id4"></span><h4>Example<a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h4>
<p>To show this resolution strategy in action, consider an example of two instances
of 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>
and one called <code class="docutils literal notranslate"><span class="pre">'publisher-polls'</span></code>. Assume we have enhanced that application
so that it takes the instance namespace into consideration when creating and
displaying polls.</p>
<div class="highlight-default snippet"><div class="snippet-filename">urls.py</div>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
<span class="p">]</span>
</pre></div>
</div>
<div class="highlight-default snippet"><div class="snippet-filename">polls/urls.py</div>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>

<span class="kn">from</span> <span class="nn">.</span> <span class="k">import</span> <span class="n">views</span>

<span class="n">app_name</span> <span class="o">=</span> <span class="s1">&#39;polls&#39;</span>
<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^(?P&lt;pk&gt;\d+)/$&#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>
    <span class="o">...</span>
<span class="p">]</span>
</pre></div>
</div>
<p>Using this setup, the following lookups are possible:</p>
<ul>
<li><p class="first">If one of the instances is current - say, if we were rendering the detail page
in 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
index page of the <code class="docutils literal notranslate"><span class="pre">'author-polls'</span></code> instance; i.e. both of the following will
result in <code class="docutils literal notranslate"><span class="pre">&quot;/author-polls/&quot;</span></code>.</p>
<p>In the method of a class-based view:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><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>
</pre></div>
</div>
<p>and in the template:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">&#39;polls:index&#39;</span> <span class="cp">%}</span>
</pre></div>
</div>
</li>
<li><p class="first">If there is no current instance - say, if we were rendering a page
somewhere else on the site - <code class="docutils literal notranslate"><span class="pre">'polls:index'</span></code> will resolve to the last
registered instance of <code class="docutils literal notranslate"><span class="pre">polls</span></code>. Since there is no default instance
(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
registered will be used. This would be <code class="docutils literal notranslate"><span class="pre">'publisher-polls'</span></code> since it’s
declared last in the <code class="docutils literal notranslate"><span class="pre">urlpatterns</span></code>.</p>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">'author-polls:index'</span></code> will always resolve to the index page of the instance
<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>
</ul>
<p>If there were also a default instance - i.e., an instance named <code class="docutils literal notranslate"><span class="pre">'polls'</span></code> -
the only change from above would be in the case where there is no current
instance (the second item in the list above). In this case <code class="docutils literal notranslate"><span class="pre">'polls:index'</span></code>
would resolve to the index page of the default instance instead of the instance
declared last in <code class="docutils literal notranslate"><span class="pre">urlpatterns</span></code>.</p>
</div>
</div>
<div class="section" id="s-url-namespaces-and-included-urlconfs">
<span id="s-namespaces-and-include"></span><span id="url-namespaces-and-included-urlconfs"></span><span id="namespaces-and-include"></span><h3>URL namespaces and included URLconfs<a class="headerlink" href="#url-namespaces-and-included-urlconfs" title="Permalink to this headline">¶</a></h3>
<p>Application namespaces of included URLconfs can be specified in two ways.</p>
<p>Firstly, you can set an <code class="docutils literal notranslate"><span class="pre">app_name</span></code> attribute in the included URLconf module,
at the same level as the <code class="docutils literal notranslate"><span class="pre">urlpatterns</span></code> attribute. You have to pass the actual
module, or a string reference to the module, to
<a class="reference internal" href="../../ref/urls.html#django.conf.urls.include" title="django.conf.urls.include"><code class="xref py py-func docutils literal notranslate"><span class="pre">include()</span></code></a>, not the list of <code class="docutils literal notranslate"><span class="pre">urlpatterns</span></code> itself.</p>
<div class="highlight-default snippet"><div class="snippet-filename">polls/urls.py</div>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">url</span>

<span class="kn">from</span> <span class="nn">.</span> <span class="k">import</span> <span class="n">views</span>

<span class="n">app_name</span> <span class="o">=</span> <span class="s1">&#39;polls&#39;</span>
<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^(?P&lt;pk&gt;\d+)/$&#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>
    <span class="o">...</span>
<span class="p">]</span>
</pre></div>
</div>
<div class="highlight-default snippet"><div class="snippet-filename">urls.py</div>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">url</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
<span class="p">]</span>
</pre></div>
</div>
<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>
<p>Secondly, you can include an object that contains embedded namespace data. If
you <code class="docutils literal notranslate"><span class="pre">include()</span></code> a list of <a class="reference internal" href="../../ref/urls.html#django.conf.urls.url" title="django.conf.urls.url"><code class="xref py py-func docutils literal notranslate"><span class="pre">url()</span></code></a> instances,
the URLs contained in that object will be added to the global namespace.
However, you can also <code class="docutils literal notranslate"><span class="pre">include()</span></code> a 2-tuple containing:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="o">&lt;</span><span class="nb">list</span> <span class="n">of</span> <span class="n">url</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>
</pre></div>
</div>
<p>For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf.urls</span> <span class="k">import</span> <span class="n">include</span><span class="p">,</span> <span class="n">url</span>

<span class="kn">from</span> <span class="nn">.</span> <span class="k">import</span> <span class="n">views</span>

<span class="n">polls_patterns</span> <span class="o">=</span> <span class="p">([</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^(?P&lt;pk&gt;\d+)/$&#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>
<span class="p">],</span> <span class="s1">&#39;polls&#39;</span><span class="p">)</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">url</span><span class="p">(</span><span class="sa">r</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>
<span class="p">]</span>
</pre></div>
</div>
<p>This will include the nominated URL patterns into the given application
namespace.</p>
<p>The instance namespace can be specified using the <code class="docutils literal notranslate"><span class="pre">namespace</span></code> argument to
<a class="reference internal" href="../../ref/urls.html#django.conf.urls.include" title="django.conf.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,
it will default to the included URLconf’s application namespace. This means
it will also be the default instance for that namespace.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">URL dispatcher</a><ul>
<li><a class="reference internal" href="#overview">Overview</a></li>
<li><a class="reference internal" href="#how-django-processes-a-request">How Django processes a request</a></li>
<li><a class="reference internal" href="#example">Example</a></li>
<li><a class="reference internal" href="#named-groups">Named groups</a><ul>
<li><a class="reference internal" href="#the-matching-grouping-algorithm">The matching/grouping algorithm</a></li>
</ul>
</li>
<li><a class="reference internal" href="#what-the-urlconf-searches-against">What the URLconf searches against</a></li>
<li><a class="reference internal" href="#captured-arguments-are-always-strings">Captured arguments are always strings</a></li>
<li><a class="reference internal" href="#specifying-defaults-for-view-arguments">Specifying defaults for view arguments</a></li>
<li><a class="reference internal" href="#performance">Performance</a></li>
<li><a class="reference internal" href="#syntax-of-the-urlpatterns-variable">Syntax of the <code class="docutils literal notranslate"><span class="pre">urlpatterns</span></code> variable</a></li>
<li><a class="reference internal" href="#error-handling">Error handling</a></li>
<li><a class="reference internal" href="#including-other-urlconfs">Including other URLconfs</a><ul>
<li><a class="reference internal" href="#captured-parameters">Captured parameters</a></li>
</ul>
</li>
<li><a class="reference internal" href="#nested-arguments">Nested arguments</a></li>
<li><a class="reference internal" href="#passing-extra-options-to-view-functions">Passing extra options to view functions</a><ul>
<li><a class="reference internal" href="#passing-extra-options-to-include">Passing extra options to <code class="docutils literal notranslate"><span class="pre">include()</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#reverse-resolution-of-urls">Reverse resolution of URLs</a><ul>
<li><a class="reference internal" href="#examples">Examples</a></li>
</ul>
</li>
<li><a class="reference internal" href="#naming-url-patterns">Naming URL patterns</a></li>
<li><a class="reference internal" href="#url-namespaces">URL namespaces</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#reversing-namespaced-urls">Reversing namespaced URLs</a><ul>
<li><a class="reference internal" href="#id4">Example</a></li>
</ul>
</li>
<li><a class="reference internal" href="#url-namespaces-and-included-urlconfs">URL namespaces and included URLconfs</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="index.html"
                        title="previous chapter">Handling HTTP requests</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="views.html"
                        title="next chapter">Writing views</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/topics/http/urls.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Feb 11, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="index.html" title="Handling HTTP requests">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="views.html" title="Writing views">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>