<!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>The redirects app — Django 1.4.11 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.4.11', 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.4.11 documentation" href="../../index.html" /> <link rel="up" title="contrib packages" href="index.html" /> <link rel="next" title="The sitemap framework" href="sitemaps.html" /> <link rel="prev" title="The messages framework" href="messages.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.4.11 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="messages.html" title="The messages framework">previous</a> | <a href="../index.html" title="API Reference" accesskey="U">up</a> | <a href="sitemaps.html" title="The sitemap framework">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="ref-contrib-redirects"> <div class="section" id="s-module-django.contrib.redirects"> <span id="s-the-redirects-app"></span><span id="module-django.contrib.redirects"></span><span id="the-redirects-app"></span><h1>The redirects app<a class="headerlink" href="#module-django.contrib.redirects" title="Permalink to this headline">¶</a></h1> <p>Django comes with an optional redirects application. It lets you store simple redirects in a database and handles the redirecting for you.</p> <div class="section" id="s-installation"> <span id="installation"></span><h2>Installation<a class="headerlink" href="#installation" title="Permalink to this headline">¶</a></h2> <p>To install the redirects app, follow these steps:</p> <ol class="arabic simple"> <li>Add <tt class="docutils literal"><span class="pre">'django.contrib.redirects'</span></tt> to your <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><tt class="xref std std-setting docutils literal"><span class="pre">INSTALLED_APPS</span></tt></a> setting.</li> <li>Add <tt class="docutils literal"><span class="pre">'django.contrib.redirects.middleware.RedirectFallbackMiddleware'</span></tt> to your <a class="reference internal" href="../settings.html#std:setting-MIDDLEWARE_CLASSES"><tt class="xref std std-setting docutils literal"><span class="pre">MIDDLEWARE_CLASSES</span></tt></a> setting.</li> <li>Run the command <a class="reference internal" href="../django-admin.html#django-admin-syncdb"><tt class="xref std std-djadmin docutils literal"><span class="pre">manage.py</span> <span class="pre">syncdb</span></tt></a>.</li> </ol> </div> <div class="section" id="s-how-it-works"> <span id="how-it-works"></span><h2>How it works<a class="headerlink" href="#how-it-works" title="Permalink to this headline">¶</a></h2> <p><tt class="docutils literal"><span class="pre">manage.py</span> <span class="pre">syncdb</span></tt> creates a <tt class="docutils literal"><span class="pre">django_redirect</span></tt> table in your database. This is a simple lookup table with <tt class="docutils literal"><span class="pre">site_id</span></tt>, <tt class="docutils literal"><span class="pre">old_path</span></tt> and <tt class="docutils literal"><span class="pre">new_path</span></tt> fields.</p> <p>The <tt class="docutils literal"><span class="pre">RedirectFallbackMiddleware</span></tt> does all of the work. Each time any Django application raises a 404 error, this middleware checks the redirects database for the requested URL as a last resort. Specifically, it checks for a redirect with the given <tt class="docutils literal"><span class="pre">old_path</span></tt> with a site ID that corresponds to the <a class="reference internal" href="../settings.html#std:setting-SITE_ID"><tt class="xref std std-setting docutils literal"><span class="pre">SITE_ID</span></tt></a> setting.</p> <ul class="simple"> <li>If it finds a match, and <tt class="docutils literal"><span class="pre">new_path</span></tt> is not empty, it redirects to <tt class="docutils literal"><span class="pre">new_path</span></tt>.</li> <li>If it finds a match, and <tt class="docutils literal"><span class="pre">new_path</span></tt> is empty, it sends a 410 (“Gone”) HTTP header and empty (content-less) response.</li> <li>If it doesn’t find a match, the request continues to be processed as usual.</li> </ul> <p>The middleware only gets activated for 404s – not for 500s or responses of any other status code.</p> <p>Note that the order of <a class="reference internal" href="../settings.html#std:setting-MIDDLEWARE_CLASSES"><tt class="xref std std-setting docutils literal"><span class="pre">MIDDLEWARE_CLASSES</span></tt></a> matters. Generally, you can put <tt class="docutils literal"><span class="pre">RedirectFallbackMiddleware</span></tt> at the end of the list, because it’s a last resort.</p> <p>For more on middleware, read the <a class="reference internal" href="../../topics/http/middleware.html"><em>middleware docs</em></a>.</p> </div> <div class="section" id="s-how-to-add-change-and-delete-redirects"> <span id="how-to-add-change-and-delete-redirects"></span><h2>How to add, change and delete redirects<a class="headerlink" href="#how-to-add-change-and-delete-redirects" title="Permalink to this headline">¶</a></h2> <div class="section" id="s-via-the-admin-interface"> <span id="via-the-admin-interface"></span><h3>Via the admin interface<a class="headerlink" href="#via-the-admin-interface" title="Permalink to this headline">¶</a></h3> <p>If you’ve activated the automatic Django admin interface, you should see a “Redirects” section on the admin index page. Edit redirects as you edit any other object in the system.</p> </div> <div class="section" id="s-via-the-python-api"> <span id="via-the-python-api"></span><h3>Via the Python API<a class="headerlink" href="#via-the-python-api" title="Permalink to this headline">¶</a></h3> <dl class="class"> <dt id="django.contrib.redirects.models.Redirect"> <em class="property">class </em><tt class="descclassname">models.</tt><tt class="descname">Redirect</tt><a class="headerlink" href="#django.contrib.redirects.models.Redirect" title="Permalink to this definition">¶</a></dt> <dd><p>Redirects are represented by a standard <a class="reference internal" href="../../topics/db/models.html"><em>Django model</em></a>, which lives in <a class="reference external" href="https://code.djangoproject.com/browser/django/trunk/django/contrib/redirects/models.py">django/contrib/redirects/models.py</a>. You can access redirect objects via the <a class="reference internal" href="../../topics/db/queries.html"><em>Django database API</em></a>.</p> </dd></dl> </div> </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="#">The redirects app</a><ul> <li><a class="reference internal" href="#installation">Installation</a></li> <li><a class="reference internal" href="#how-it-works">How it works</a></li> <li><a class="reference internal" href="#how-to-add-change-and-delete-redirects">How to add, change and delete redirects</a><ul> <li><a class="reference internal" href="#via-the-admin-interface">Via the admin interface</a></li> <li><a class="reference internal" href="#via-the-python-api">Via the Python API</a></li> </ul> </li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="messages.html">The messages framework</a></li> <li>Next: <a href="sitemaps.html">The sitemap framework</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../../index.html">Django 1.4.11 documentation</a> <ul><li><a href="../index.html">API Reference</a> <ul><li><a href="index.html"><tt class="docutils literal"><span class="pre">contrib</span></tt> packages</a> <ul><li>The redirects app</li></ul> </li></ul></li></ul> </li> </ul> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/ref/contrib/redirects.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">Apr 24, 2014</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="messages.html" title="The messages framework">previous</a> | <a href="../index.html" title="API Reference" accesskey="U">up</a> | <a href="sitemaps.html" title="The sitemap framework">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>