2005-11-11 12:45:05 +08:00
|
|
|
=================
|
|
|
|
The redirects app
|
|
|
|
=================
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. module:: django.contrib.redirects
|
|
|
|
:synopsis: A framework for managing redirects.
|
|
|
|
|
2005-11-11 12:45:05 +08:00
|
|
|
Django comes with an optional redirects application. It lets you store simple
|
|
|
|
redirects in a database and handles the redirecting for you.
|
|
|
|
|
|
|
|
Installation
|
|
|
|
============
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
To install the redirects app, follow these steps:
|
2005-11-11 12:45:05 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
1. Add ``'django.contrib.redirects'`` to your :setting:`INSTALLED_APPS`
|
|
|
|
setting.
|
|
|
|
2. Add ``'django.contrib.redirects.middleware.RedirectFallbackMiddleware'``
|
|
|
|
to your :setting:`MIDDLEWARE_CLASSES` setting.
|
|
|
|
3. Run the command :djadmin:`manage.py syncdb <syncdb>`.
|
2005-11-11 12:45:05 +08:00
|
|
|
|
|
|
|
How it works
|
|
|
|
============
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
``manage.py syncdb`` creates a ``django_redirect`` table in your database. This
|
|
|
|
is a simple lookup table with ``site_id``, ``old_path`` and ``new_path`` fields.
|
2005-11-11 12:45:05 +08:00
|
|
|
|
|
|
|
The ``RedirectFallbackMiddleware`` 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
|
2008-08-24 06:25:40 +08:00
|
|
|
with the given ``old_path`` with a site ID that corresponds to the
|
|
|
|
:setting:`SITE_ID` setting.
|
2005-11-11 12:45:05 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* If it finds a match, and ``new_path`` is not empty, it redirects to
|
|
|
|
``new_path``.
|
|
|
|
* If it finds a match, and ``new_path`` is empty, it sends a 410 ("Gone")
|
|
|
|
HTTP header and empty (content-less) response.
|
|
|
|
* If it doesn't find a match, the request continues to be processed as
|
|
|
|
usual.
|
2005-11-11 12:45:05 +08:00
|
|
|
|
|
|
|
The middleware only gets activated for 404s -- not for 500s or responses of any
|
|
|
|
other status code.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Note that the order of :setting:`MIDDLEWARE_CLASSES` matters. Generally, you
|
|
|
|
can put ``RedirectFallbackMiddleware`` at the end of the list, because it's a
|
|
|
|
last resort.
|
2005-11-11 12:45:05 +08:00
|
|
|
|
2010-08-20 03:27:44 +08:00
|
|
|
For more on middleware, read the :doc:`middleware docs
|
|
|
|
</topics/http/middleware>`.
|
2005-11-11 12:45:05 +08:00
|
|
|
|
|
|
|
How to add, change and delete redirects
|
|
|
|
=======================================
|
|
|
|
|
|
|
|
Via the admin interface
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
Via the Python API
|
|
|
|
------------------
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: models.Redirect
|
|
|
|
|
2010-08-20 03:27:44 +08:00
|
|
|
Redirects are represented by a standard :doc:`Django model </topics/db/models>`,
|
2008-08-24 06:25:40 +08:00
|
|
|
which lives in `django/contrib/redirects/models.py`_. You can access redirect
|
2010-08-20 03:27:44 +08:00
|
|
|
objects via the :doc:`Django database API </topics/db/queries>`.
|
2005-11-11 12:45:05 +08:00
|
|
|
|
2012-05-03 23:42:56 +08:00
|
|
|
.. _django/contrib/redirects/models.py: https://github.com/django/django/blob/master/django/contrib/redirects/models.py
|