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.
|
|
|
|
|
2019-06-17 22:54:55 +08:00
|
|
|
Django comes with an optional redirects application. It lets you store
|
2013-11-02 02:55:40 +08:00
|
|
|
redirects in a database and handles the redirecting for you. It uses the HTTP
|
|
|
|
response status code ``301 Moved Permanently`` by default.
|
2005-11-11 12:45:05 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2018-11-16 02:54:28 +08:00
|
|
|
#. Ensure that the ``django.contrib.sites`` framework
|
2013-02-08 23:32:09 +08:00
|
|
|
:ref:`is installed <enabling-the-sites-framework>`.
|
2018-11-16 02:54:28 +08:00
|
|
|
#. Add ``'django.contrib.redirects'`` to your :setting:`INSTALLED_APPS` setting.
|
|
|
|
#. Add ``'django.contrib.redirects.middleware.RedirectFallbackMiddleware'``
|
2015-11-07 23:12:37 +08:00
|
|
|
to your :setting:`MIDDLEWARE` setting.
|
2018-11-16 02:54:28 +08:00
|
|
|
#. Run the command :djadmin:`manage.py migrate <migrate>`.
|
2005-11-11 12:45:05 +08:00
|
|
|
|
|
|
|
How it works
|
|
|
|
============
|
|
|
|
|
2013-07-25 23:19:36 +08:00
|
|
|
``manage.py migrate`` creates a ``django_redirect`` table in your database. This
|
2019-06-17 22:54:55 +08:00
|
|
|
is a lookup table with ``site_id``, ``old_path`` and ``new_path`` fields.
|
2005-11-11 12:45:05 +08:00
|
|
|
|
2013-05-21 03:22:38 +08:00
|
|
|
The :class:`~django.contrib.redirects.middleware.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 with the
|
|
|
|
given ``old_path`` with a site ID that corresponds to the
|
2008-08-24 06:25:40 +08:00
|
|
|
: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
|
2013-11-02 02:55:40 +08:00
|
|
|
``new_path`` using a 301 ("Moved Permanently") redirect. You can subclass
|
|
|
|
:class:`~django.contrib.redirects.middleware.RedirectFallbackMiddleware`
|
|
|
|
and set
|
|
|
|
:attr:`~django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_redirect_class`
|
|
|
|
to :class:`django.http.HttpResponseRedirect` to use a
|
|
|
|
``302 Moved Temporarily`` redirect instead.
|
2011-10-14 08:12:01 +08:00
|
|
|
* 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.
|
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
Note that the order of :setting:`MIDDLEWARE` matters. Generally, you can put
|
|
|
|
:class:`~django.contrib.redirects.middleware.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>`,
|
2019-03-29 08:32:17 +08:00
|
|
|
which lives in :source:`django/contrib/redirects/models.py`. You can access
|
|
|
|
redirect objects via the :doc:`Django database API </topics/db/queries>`.
|
2020-06-23 18:19:51 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
>>> from django.conf import settings
|
|
|
|
>>> from django.contrib.redirects.models import Redirect
|
|
|
|
>>> # Add a new redirect.
|
|
|
|
>>> redirect = Redirect.objects.create(
|
|
|
|
... site_id=1,
|
|
|
|
... old_path='/contact-us/',
|
|
|
|
... new_path='/contact/',
|
|
|
|
... )
|
|
|
|
>>> # Change a redirect.
|
|
|
|
>>> redirect.new_path = '/contact-details/'
|
|
|
|
>>> redirect.save()
|
|
|
|
>>> redirect
|
|
|
|
<Redirect: /contact-us/ ---> /contact-details/>
|
|
|
|
>>> # Delete a redirect.
|
|
|
|
>>> Redirect.objects.filter(site_id=1, old_path='/contact-us/').delete()
|
|
|
|
(1, {'redirects.Redirect': 1})
|
2013-05-21 03:22:38 +08:00
|
|
|
|
|
|
|
Middleware
|
|
|
|
==========
|
|
|
|
|
|
|
|
.. class:: middleware.RedirectFallbackMiddleware
|
|
|
|
|
|
|
|
You can change the :class:`~django.http.HttpResponse` classes used
|
|
|
|
by the middleware by creating a subclass of
|
|
|
|
:class:`~django.contrib.redirects.middleware.RedirectFallbackMiddleware`
|
|
|
|
and overriding ``response_gone_class`` and/or ``response_redirect_class``.
|
|
|
|
|
|
|
|
.. attribute:: response_gone_class
|
|
|
|
|
|
|
|
The :class:`~django.http.HttpResponse` class used when a
|
2015-01-27 04:39:52 +08:00
|
|
|
:class:`~django.contrib.redirects.models.Redirect` is not found for the
|
|
|
|
requested path or has a blank ``new_path`` value.
|
2013-05-21 03:22:38 +08:00
|
|
|
|
|
|
|
Defaults to :class:`~django.http.HttpResponseGone`.
|
|
|
|
|
|
|
|
.. attribute:: response_redirect_class
|
|
|
|
|
2015-01-27 04:39:52 +08:00
|
|
|
The :class:`~django.http.HttpResponse` class that handles the redirect.
|
2013-05-21 03:22:38 +08:00
|
|
|
|
|
|
|
Defaults to :class:`~django.http.HttpResponsePermanentRedirect`.
|