<!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>django.conf.urls utility functions — Django 1.5.9 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.5.9', 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="top" title="Django 1.5.9 documentation" href="../index.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> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../index.html">Django 1.5.9 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="<tt class="docutils literal"><span class="pre">django.core.urlresolvers</span></tt> 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><tt class="docutils literal"><span class="pre">django.conf.urls</span></tt> utility functions<a class="headerlink" href="#module-django.conf.urls" title="Permalink to this headline">¶</a></h1> <div class="versionchanged"> <span class="title">Changed in Django 1.4:</span> Starting with Django 1.4 functions <tt class="docutils literal"><span class="pre">patterns</span></tt>, <tt class="docutils literal"><span class="pre">url</span></tt>, <tt class="docutils literal"><span class="pre">include</span></tt> plus the <tt class="docutils literal"><span class="pre">handler*</span></tt> symbols described below live in the <tt class="docutils literal"><span class="pre">django.conf.urls</span></tt> module.<p>Until Django 1.3 they were located in <tt class="docutils literal"><span class="pre">django.conf.urls.defaults</span></tt>. You still can import them from there but it will be removed in Django 1.6.</p> </div> <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"> <tt class="descname">patterns</tt>(<em>prefix</em>, <em>pattern_description</em>, <em>...</em>)<a class="headerlink" href="#django.conf.urls.patterns" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <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 <tt class="docutils literal"><span class="pre">patterns()</span></tt> is a string <tt class="docutils literal"><span class="pre">prefix</span></tt>. See <a class="reference internal" href="../topics/http/urls.html#urlpatterns-view-prefix"><em>The view prefix</em></a>.</p> <p>The remaining arguments should be tuples in this format:</p> <div class="highlight-python"><pre>(regular expression, Python callback function [, optional_dictionary [, optional_name]])</pre> </div> <p>The <tt class="docutils literal"><span class="pre">optional_dictionary</span></tt> and <tt class="docutils literal"><span class="pre">optional_name</span></tt> parameters are described in <a class="reference internal" href="../topics/http/urls.html#views-extra-options"><em>Passing extra options to view functions</em></a>.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p>Because <tt class="docutils literal"><span class="pre">patterns()</span></tt> 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 <tt class="docutils literal"><span class="pre">include()</span></tt> sections. However, on the off-chance you do hit the 255-argument limit, realize that <tt class="docutils literal"><span class="pre">patterns()</span></tt> returns a Python list, so you can split up the construction of the list.</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</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="s">''</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"> <tt class="descclassname">static.</tt><tt class="descname">static</tt>(<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-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span> <span class="kn">from</span> <span class="nn">django.conf.urls.static</span> <span class="kn">import</span> <span class="n">static</span> <span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span> <span class="c"># ... 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> <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"> <tt class="descname">url</tt>(<em>regex</em>, <em>view</em>, <em>kwargs=None</em>, <em>name=None</em>, <em>prefix=''</em>)<a class="headerlink" href="#django.conf.urls.url" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>You can use the <tt class="docutils literal"><span class="pre">url()</span></tt> function, instead of a tuple, as an argument to <tt class="docutils literal"><span class="pre">patterns()</span></tt>. This is convenient if you want to specify a name without the optional extra arguments dictionary. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span> <span class="n">url</span><span class="p">(</span><span class="s">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="s">"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-python"><div class="highlight"><pre><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="bp">None</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s">''</span><span class="p">)</span> </pre></div> </div> <p>See <a class="reference internal" href="../topics/http/urls.html#naming-url-patterns"><em>Naming URL patterns</em></a> for why the <tt class="docutils literal"><span class="pre">name</span></tt> parameter is useful.</p> <p>The <tt class="docutils literal"><span class="pre">prefix</span></tt> parameter has the same meaning as the first argument to <tt class="docutils literal"><span class="pre">patterns()</span></tt> and is only relevant when you’re passing a string as the <tt class="docutils literal"><span class="pre">view</span></tt> parameter.</p> </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"> <tt class="descname">include</tt>(<em>module</em><span class="optional">[</span>, <em>namespace=None</em>, <em>app_name=None</em><span class="optional">]</span>)<a class="headerlink" href="#django.conf.urls.include" title="Permalink to this definition">¶</a></dt> <dt> <tt class="descname">include</tt>(<em>pattern_list</em>)</dt> <dt> <tt class="descname">include</tt>(<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"><em class="xref std std-term">application namespace</em></a> and <a class="reference internal" href="../topics/http/urls.html#term-instance-namespace"><em class="xref std std-term">instance namespace</em></a> where the entries will be included into can also be specified.</p> <p><tt class="docutils literal"><span class="pre">include()</span></tt> 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> (<a class="reference external" href="http://docs.python.org/2.7/library/string.html#module-string" title="(in Python v2.7)"><em>string</em></a>) – Instance namespace for the URL entries being included</li> <li><strong>app_name</strong> (<a class="reference external" href="http://docs.python.org/2.7/library/string.html#module-string" title="(in Python v2.7)"><em>string</em></a>) – Application namespace for the URL entries being included</li> <li><strong>pattern_list</strong> – Iterable of URL entries as returned by <a class="reference internal" href="#django.conf.urls.patterns" title="django.conf.urls.patterns"><tt class="xref py py-func docutils literal"><span class="pre">patterns()</span></tt></a></li> <li><strong>app_namespace</strong> (<a class="reference external" href="http://docs.python.org/2.7/library/string.html#module-string" title="(in Python v2.7)"><em>string</em></a>) – Application namespace for the URL entries being included</li> <li><strong>instance_namespace</strong> (<a class="reference external" href="http://docs.python.org/2.7/library/string.html#module-string" title="(in Python v2.7)"><em>string</em></a>) – 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"><em>Including other URLconfs</em></a> and <a class="reference internal" href="../topics/http/urls.html#namespaces-and-include"><em>URL namespaces and included URLconfs</em></a>.</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"> <tt class="descname">handler403</tt><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 <tt class="docutils literal"><span class="pre">'django.views.defaults.permission_denied'</span></tt>. That default value should suffice.</p> <p>See the documentation about <a class="reference internal" href="../topics/http/views.html#http-forbidden-view"><em>the 403 (HTTP Forbidden) view</em></a> for more information.</p> <div class="versionadded"> <span class="title">New in Django 1.4:</span> <tt class="docutils literal"><span class="pre">handler403</span></tt> is new in Django 1.4.</div> </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"> <tt class="descname">handler404</tt><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 <tt class="docutils literal"><span class="pre">'django.views.defaults.page_not_found'</span></tt>. That default value should suffice.</p> <p>See the documentation about <a class="reference internal" href="../topics/http/views.html#http-not-found-view"><em>the 404 (HTTP Not Found) view</em></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"> <tt class="descname">handler500</tt><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 <tt class="docutils literal"><span class="pre">'django.views.defaults.server_error'</span></tt>. That default value should suffice.</p> <p>See the documentation about <a class="reference internal" href="../topics/http/views.html#http-internal-server-error-view"><em>the 500 (HTTP Internal Server Error) view</em></a> for more information.</p> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#"><tt class="docutils literal"><span class="pre">django.conf.urls</span></tt> 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="#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"><tt class="docutils literal"><span class="pre">django.core.urlresolvers</span></tt> 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.5.9 documentation</a> <ul><li><a href="index.html">API Reference</a> <ul><li><tt class="docutils literal"><span class="pre">django.conf.urls</span></tt> utility functions</li></ul> </li></ul> </li> </ul> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/ref/urls.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <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> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Jan 15, 2015</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="urlresolvers.html" title="<tt class="docutils literal"><span class="pre">django.core.urlresolvers</span></tt> 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>