<!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="Content-Type" content="text/html; charset=utf-8" /> <title>django.conf.urls utility functions — Django 1.8.19 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"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '1.8.19', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </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> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="top" title="Django 1.8.19 documentation" href="../contents.html" /> <link rel="up" title="API Reference" href="index.html" /> <link rel="next" title="Django Utils" href="utils.html" /> <link rel="prev" title="django.core.urlresolvers utility functions" href="urlresolvers.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 = "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 role="document"> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../index.html">Django 1.8.19 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"> « <a href="urlresolvers.html" title="<code class="docutils literal"><span class="pre">django.core.urlresolvers</span></code> utility functions">previous</a> | <a href="index.html" title="API Reference" accesskey="U">up</a> | <a href="utils.html" title="Django Utils">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="ref-urls"> <div class="section" id="s-module-django.conf.urls"> <span id="s-django-conf-urls-utility-functions"></span><span id="module-django.conf.urls"></span><span id="django-conf-urls-utility-functions"></span><h1><code class="docutils literal"><span class="pre">django.conf.urls</span></code> utility functions<a class="headerlink" href="#module-django.conf.urls" title="Permalink to this headline">¶</a></h1> <div class="section" id="s-patterns"> <span id="patterns"></span><h2>patterns()<a class="headerlink" href="#patterns" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="django.conf.urls.patterns"> <code class="descname">patterns</code>(<em>prefix</em>, <em>pattern_description</em>, <em>...</em>)<a class="reference internal" href="../_modules/django/conf/urls.html#patterns"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.conf.urls.patterns" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 1.8: </span><code class="docutils literal"><span class="pre">urlpatterns</span></code> should be a plain list of <a class="reference internal" href="#django.conf.urls.url" title="django.conf.urls.url"><code class="xref py py-func docutils literal"><span class="pre">django.conf.urls.url()</span></code></a> instances instead.</p> </div> <p>A function that takes a prefix, and an arbitrary number of URL patterns, and returns a list of URL patterns in the format Django needs.</p> <p>The first argument to <code class="docutils literal"><span class="pre">patterns()</span></code> is a string <code class="docutils literal"><span class="pre">prefix</span></code>. Here’s the example URLconf from the <a class="reference internal" href="../intro/overview.html"><span class="doc">Django overview</span></a>:</p> <div class="highlight-default"><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">patterns</span><span class="p">,</span> <span class="n">url</span> <span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s1">''</span><span class="p">,</span> <span class="n">url</span><span class="p">(</span><span class="s1">r'^articles/([0-9]</span><span class="si">{4}</span><span class="s1">)/$'</span><span class="p">,</span> <span class="s1">'news.views.year_archive'</span><span class="p">),</span> <span class="n">url</span><span class="p">(</span><span class="s1">r'^articles/([0-9]</span><span class="si">{4}</span><span class="s1">)/([0-9]</span><span class="si">{2}</span><span class="s1">)/$'</span><span class="p">,</span> <span class="s1">'news.views.month_archive'</span><span class="p">),</span> <span class="n">url</span><span class="p">(</span><span class="s1">r'^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]+)/$'</span><span class="p">,</span> <span class="s1">'news.views.article_detail'</span><span class="p">),</span> <span class="p">)</span> </pre></div> </div> <p>In this example, each view has a common prefix – <code class="docutils literal"><span class="pre">'news.views'</span></code>. Instead of typing that out for each entry in <code class="docutils literal"><span class="pre">urlpatterns</span></code>, you can use the first argument to the <code class="docutils literal"><span class="pre">patterns()</span></code> function to specify a prefix to apply to each view function.</p> <p>With this in mind, the above example can be written more concisely as:</p> <div class="highlight-default"><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">patterns</span><span class="p">,</span> <span class="n">url</span> <span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s1">'news.views'</span><span class="p">,</span> <span class="n">url</span><span class="p">(</span><span class="s1">r'^articles/([0-9]</span><span class="si">{4}</span><span class="s1">)/$'</span><span class="p">,</span> <span class="s1">'year_archive'</span><span class="p">),</span> <span class="n">url</span><span class="p">(</span><span class="s1">r'^articles/([0-9]</span><span class="si">{4}</span><span class="s1">)/([0-9]</span><span class="si">{2}</span><span class="s1">)/$'</span><span class="p">,</span> <span class="s1">'month_archive'</span><span class="p">),</span> <span class="n">url</span><span class="p">(</span><span class="s1">r'^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]+)/$'</span><span class="p">,</span> <span class="s1">'article_detail'</span><span class="p">),</span> <span class="p">)</span> </pre></div> </div> <p>Note that you don’t put a trailing dot (<code class="docutils literal"><span class="pre">"."</span></code>) in the prefix. Django puts that in automatically.</p> <p>The remaining arguments should be tuples in this format:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">regular</span> <span class="n">expression</span><span class="p">,</span> <span class="n">Python</span> <span class="n">callback</span> <span class="n">function</span> <span class="p">[,</span> <span class="n">optional_dictionary</span> <span class="p">[,</span> <span class="n">optional_name</span><span class="p">]])</span> </pre></div> </div> <p>The <code class="docutils literal"><span class="pre">optional_dictionary</span></code> and <code class="docutils literal"><span class="pre">optional_name</span></code> parameters are described in <a class="reference internal" href="../topics/http/urls.html#views-extra-options"><span class="std std-ref">Passing extra options to view functions</span></a>.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p>Because <code class="docutils literal"><span class="pre">patterns()</span></code> is a function call, it accepts a maximum of 255 arguments (URL patterns, in this case). This is a limit for all Python function calls. This is rarely a problem in practice, because you’ll typically structure your URL patterns modularly by using <code class="docutils literal"><span class="pre">include()</span></code> sections. However, on the off-chance you do hit the 255-argument limit, realize that <code class="docutils literal"><span class="pre">patterns()</span></code> returns a Python list, so you can split up the construction of the list.</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s1">''</span><span class="p">,</span> <span class="o">...</span> <span class="p">)</span> <span class="n">urlpatterns</span> <span class="o">+=</span> <span class="n">patterns</span><span class="p">(</span><span class="s1">''</span><span class="p">,</span> <span class="o">...</span> <span class="p">)</span> </pre></div> </div> <p class="last">Python lists have unlimited size, so there’s no limit to how many URL patterns you can construct. The only limit is that you can only create 254 at a time (the 255th argument is the initial prefix argument).</p> </div> </div> <div class="section" id="s-static"> <span id="static"></span><h2>static()<a class="headerlink" href="#static" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="django.conf.urls.static.static"> <code class="descclassname">static.</code><code class="descname">static</code>(<em>prefix</em>, <em>view=django.views.static.serve</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.conf.urls.static.static" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>Helper function to return a URL pattern for serving files in debug mode:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span> <span class="kn">from</span> <span class="nn">django.conf.urls.static</span> <span class="k">import</span> <span class="n">static</span> <span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span> <span class="c1"># ... the rest of your URLconf goes here ...</span> <span class="p">]</span> <span class="o">+</span> <span class="n">static</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">MEDIA_URL</span><span class="p">,</span> <span class="n">document_root</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">MEDIA_ROOT</span><span class="p">)</span> </pre></div> </div> <div class="versionchanged"> <span class="title">Changed in Django 1.8:</span> <p>The <code class="docutils literal"><span class="pre">view</span></code> argument changed from a string (<code class="docutils literal"><span class="pre">'django.views.static.serve'</span></code>) to the function.</p> </div> </div> <div class="section" id="s-url"> <span id="url"></span><h2>url()<a class="headerlink" href="#url" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="django.conf.urls.url"> <code class="descname">url</code>(<em>regex</em>, <em>view</em>, <em>kwargs=None</em>, <em>name=None</em>, <em>prefix=''</em>)<a class="reference internal" href="../_modules/django/conf/urls.html#url"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.conf.urls.url" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p><code class="docutils literal"><span class="pre">urlpatterns</span></code> should be a list of <code class="docutils literal"><span class="pre">url()</span></code> instances. For example:</p> <div class="highlight-default"><div class="highlight"><pre><span></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="s1">r'^index/$'</span><span class="p">,</span> <span class="n">index_view</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">"main-view"</span><span class="p">),</span> <span class="o">...</span> <span class="p">]</span> </pre></div> </div> <p>This function takes five arguments, most of which are optional:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">url</span><span class="p">(</span><span class="n">regex</span><span class="p">,</span> <span class="n">view</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s1">''</span><span class="p">)</span> </pre></div> </div> <p>The <code class="docutils literal"><span class="pre">kwargs</span></code> parameter allows you to pass additional arguments to the view function or method. See <a class="reference internal" href="../topics/http/urls.html#views-extra-options"><span class="std std-ref">Passing extra options to view functions</span></a> for an example.</p> <p>See <a class="reference internal" href="../topics/http/urls.html#naming-url-patterns"><span class="std std-ref">Naming URL patterns</span></a> for why the <code class="docutils literal"><span class="pre">name</span></code> parameter is useful.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 1.8: </span>Support for string <code class="docutils literal"><span class="pre">view</span></code> arguments is deprecated and will be removed in Django 1.10. Pass the callable instead.</p> <p>The <code class="docutils literal"><span class="pre">prefix</span></code> parameter has the same meaning as the first argument to <code class="docutils literal"><span class="pre">patterns()</span></code> and is only relevant when you’re passing a string as the <code class="docutils literal"><span class="pre">view</span></code> parameter.</p> </div> </div> <div class="section" id="s-include"> <span id="include"></span><h2>include()<a class="headerlink" href="#include" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="django.conf.urls.include"> <code class="descname">include</code>(<em>module</em>, <em>namespace=None</em>, <em>app_name=None</em>)<a class="reference internal" href="../_modules/django/conf/urls.html#include"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.conf.urls.include" title="Permalink to this definition">¶</a></dt> <dt> <code class="descname">include</code>(<em>pattern_list</em>)</dt> <dt> <code class="descname">include</code>(<em>(pattern_list</em>, <em>app_namespace</em>, <em>instance_namespace)</em>)</dt> <dd><p>A function that takes a full Python import path to another URLconf module that should be “included” in this place. Optionally, the <a class="reference internal" href="../topics/http/urls.html#term-application-namespace"><span class="xref std std-term">application namespace</span></a> and <a class="reference internal" href="../topics/http/urls.html#term-instance-namespace"><span class="xref std std-term">instance namespace</span></a> where the entries will be included into can also be specified.</p> <p><code class="docutils literal"><span class="pre">include()</span></code> also accepts as an argument either an iterable that returns URL patterns or a 3-tuple containing such iterable plus the names of the application and instance namespaces.</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple"> <li><strong>module</strong> – URLconf module (or module name)</li> <li><strong>namespace</strong> (<em>string</em>) – Instance namespace for the URL entries being included</li> <li><strong>app_name</strong> (<em>string</em>) – Application namespace for the URL entries being included</li> <li><strong>pattern_list</strong> – Iterable of <a class="reference internal" href="#django.conf.urls.url" title="django.conf.urls.url"><code class="xref py py-func docutils literal"><span class="pre">django.conf.urls.url()</span></code></a> instances</li> <li><strong>app_namespace</strong> (<em>string</em>) – Application namespace for the URL entries being included</li> <li><strong>instance_namespace</strong> (<em>string</em>) – Instance namespace for the URL entries being included</li> </ul> </td> </tr> </tbody> </table> </dd></dl> <p>See <a class="reference internal" href="../topics/http/urls.html#including-other-urlconfs"><span class="std std-ref">Including other URLconfs</span></a> and <a class="reference internal" href="../topics/http/urls.html#namespaces-and-include"><span class="std std-ref">URL namespaces and included URLconfs</span></a>.</p> </div> <div class="section" id="s-handler400"> <span id="handler400"></span><h2>handler400<a class="headerlink" href="#handler400" title="Permalink to this headline">¶</a></h2> <dl class="data"> <dt id="django.conf.urls.handler400"> <code class="descname">handler400</code><a class="headerlink" href="#django.conf.urls.handler400" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>A callable, or a string representing the full Python import path to the view that should be called if the HTTP client has sent a request that caused an error condition and a response with a status code of 400.</p> <p>By default, this is <code class="docutils literal"><span class="pre">'django.views.defaults.bad_request'</span></code>. That default value should suffice.</p> <p>See the documentation about <a class="reference internal" href="views.html#http-bad-request-view"><span class="std std-ref">the 400 (bad request) view</span></a> for more information.</p> </div> <div class="section" id="s-handler403"> <span id="handler403"></span><h2>handler403<a class="headerlink" href="#handler403" title="Permalink to this headline">¶</a></h2> <dl class="data"> <dt id="django.conf.urls.handler403"> <code class="descname">handler403</code><a class="headerlink" href="#django.conf.urls.handler403" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>A callable, or a string representing the full Python import path to the view that should be called if the user doesn’t have the permissions required to access a resource.</p> <p>By default, this is <code class="docutils literal"><span class="pre">'django.views.defaults.permission_denied'</span></code>. That default value should suffice.</p> <p>See the documentation about <a class="reference internal" href="views.html#http-forbidden-view"><span class="std std-ref">the 403 (HTTP Forbidden) view</span></a> for more information.</p> </div> <div class="section" id="s-handler404"> <span id="handler404"></span><h2>handler404<a class="headerlink" href="#handler404" title="Permalink to this headline">¶</a></h2> <dl class="data"> <dt id="django.conf.urls.handler404"> <code class="descname">handler404</code><a class="headerlink" href="#django.conf.urls.handler404" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>A callable, or a string representing the full Python import path to the view that should be called if none of the URL patterns match.</p> <p>By default, this is <code class="docutils literal"><span class="pre">'django.views.defaults.page_not_found'</span></code>. That default value should suffice.</p> <p>See the documentation about <a class="reference internal" href="views.html#http-not-found-view"><span class="std std-ref">the 404 (HTTP Not Found) view</span></a> for more information.</p> </div> <div class="section" id="s-handler500"> <span id="handler500"></span><h2>handler500<a class="headerlink" href="#handler500" title="Permalink to this headline">¶</a></h2> <dl class="data"> <dt id="django.conf.urls.handler500"> <code class="descname">handler500</code><a class="headerlink" href="#django.conf.urls.handler500" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>A callable, or a string representing the full Python import path to the view that should be called in case of server errors. Server errors happen when you have runtime errors in view code.</p> <p>By default, this is <code class="docutils literal"><span class="pre">'django.views.defaults.server_error'</span></code>. That default value should suffice.</p> <p>See the documentation about <a class="reference internal" href="views.html#http-internal-server-error-view"><span class="std std-ref">the 500 (HTTP Internal Server Error) view</span></a> for more information.</p> </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="#"><code class="docutils literal"><span class="pre">django.conf.urls</span></code> utility functions</a><ul> <li><a class="reference internal" href="#patterns">patterns()</a></li> <li><a class="reference internal" href="#static">static()</a></li> <li><a class="reference internal" href="#url">url()</a></li> <li><a class="reference internal" href="#include">include()</a></li> <li><a class="reference internal" href="#handler400">handler400</a></li> <li><a class="reference internal" href="#handler403">handler403</a></li> <li><a class="reference internal" href="#handler404">handler404</a></li> <li><a class="reference internal" href="#handler500">handler500</a></li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="urlresolvers.html"><code class="docutils literal"><span class="pre">django.core.urlresolvers</span></code> utility functions</a></li> <li>Next: <a href="utils.html">Django Utils</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../index.html">Django 1.8.19 documentation</a> <ul><li><a href="index.html">API Reference</a> <ul><li><code class="docutils literal"><span class="pre">django.conf.urls</span></code> utility functions</li></ul> </li></ul> </li> </ul> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/ref/urls.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <div><input type="text" name="q" /></div> <div><input type="submit" value="Go" /></div> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Mar 10, 2018</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="urlresolvers.html" title="<code class="docutils literal"><span class="pre">django.core.urlresolvers</span></code> utility functions">previous</a> | <a href="index.html" title="API Reference" accesskey="U">up</a> | <a href="utils.html" title="Django Utils">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>