redirects.txt 2.5 KB

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