redirects.txt 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. =================
  2. The redirects app
  3. =================
  4. .. module:: django.contrib.redirects
  5. :synopsis: A framework for managing redirects.
  6. Django comes with an optional redirects application. It lets you store simple
  7. redirects in a database and handles the redirecting for you.
  8. Installation
  9. ============
  10. To install the redirects app, follow these steps:
  11. 1. Ensure that the ``django.contrib.sites`` framework
  12. :ref:`is installed <enabling-the-sites-framework>`.
  13. 2. Add ``'django.contrib.redirects'`` to your :setting:`INSTALLED_APPS` setting.
  14. 3. Add ``'django.contrib.redirects.middleware.RedirectFallbackMiddleware'``
  15. to your :setting:`MIDDLEWARE_CLASSES` setting.
  16. 4. Run the command :djadmin:`manage.py syncdb <syncdb>`.
  17. How it works
  18. ============
  19. ``manage.py syncdb`` creates a ``django_redirect`` table in your database. This
  20. is a simple lookup table with ``site_id``, ``old_path`` and ``new_path`` fields.
  21. The ``RedirectFallbackMiddleware`` does all of the work. Each time any Django
  22. application raises a 404 error, this middleware checks the redirects database
  23. for the requested URL as a last resort. Specifically, it checks for a redirect
  24. with the given ``old_path`` with a site ID that corresponds to the
  25. :setting:`SITE_ID` setting.
  26. * If it finds a match, and ``new_path`` is not empty, it redirects to
  27. ``new_path``.
  28. * If it finds a match, and ``new_path`` is empty, it sends a 410 ("Gone")
  29. HTTP header and empty (content-less) response.
  30. * If it doesn't find a match, the request continues to be processed as
  31. usual.
  32. The middleware only gets activated for 404s -- not for 500s or responses of any
  33. other status code.
  34. Note that the order of :setting:`MIDDLEWARE_CLASSES` matters. Generally, you
  35. can put ``RedirectFallbackMiddleware`` at the end of the list, because it's a
  36. last resort.
  37. For more on middleware, read the :doc:`middleware docs
  38. </topics/http/middleware>`.
  39. How to add, change and delete redirects
  40. =======================================
  41. Via the admin interface
  42. -----------------------
  43. If you've activated the automatic Django admin interface, you should see a
  44. "Redirects" section on the admin index page. Edit redirects as you edit any
  45. other object in the system.
  46. Via the Python API
  47. ------------------
  48. .. class:: models.Redirect
  49. Redirects are represented by a standard :doc:`Django model </topics/db/models>`,
  50. which lives in `django/contrib/redirects/models.py`_. You can access redirect
  51. objects via the :doc:`Django database API </topics/db/queries>`.
  52. .. _django/contrib/redirects/models.py: https://github.com/django/django/blob/master/django/contrib/redirects/models.py