redirects.txt 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 migrate <migrate>`.
  17. How it works
  18. ============
  19. ``manage.py migrate`` 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 :class:`~django.contrib.redirects.middleware.RedirectFallbackMiddleware`
  22. does all of the work. Each time any Django application raises a 404
  23. error, this middleware checks the redirects database for the requested
  24. URL as a last resort. Specifically, it checks for a redirect with the
  25. given ``old_path`` with a site ID that corresponds to the
  26. :setting:`SITE_ID` setting.
  27. * If it finds a match, and ``new_path`` is not empty, it redirects to
  28. ``new_path``.
  29. * If it finds a match, and ``new_path`` is empty, it sends a 410 ("Gone")
  30. HTTP header and empty (content-less) response.
  31. * If it doesn't find a match, the request continues to be processed as
  32. usual.
  33. The middleware only gets activated for 404s -- not for 500s or responses of any
  34. other status code.
  35. Note that the order of :setting:`MIDDLEWARE_CLASSES` matters. Generally, you
  36. can put :class:`~django.contrib.redirects.middleware.RedirectFallbackMiddleware`
  37. at the end of the list, because it's a last resort.
  38. For more on middleware, read the :doc:`middleware docs
  39. </topics/http/middleware>`.
  40. How to add, change and delete redirects
  41. =======================================
  42. Via the admin interface
  43. -----------------------
  44. If you've activated the automatic Django admin interface, you should see a
  45. "Redirects" section on the admin index page. Edit redirects as you edit any
  46. other object in the system.
  47. Via the Python API
  48. ------------------
  49. .. class:: models.Redirect
  50. Redirects are represented by a standard :doc:`Django model </topics/db/models>`,
  51. which lives in `django/contrib/redirects/models.py`_. You can access redirect
  52. objects via the :doc:`Django database API </topics/db/queries>`.
  53. .. _django/contrib/redirects/models.py: https://github.com/django/django/blob/master/django/contrib/redirects/models.py
  54. Middleware
  55. ==========
  56. .. class:: middleware.RedirectFallbackMiddleware
  57. You can change the :class:`~django.http.HttpResponse` classes used
  58. by the middleware by creating a subclass of
  59. :class:`~django.contrib.redirects.middleware.RedirectFallbackMiddleware`
  60. and overriding ``response_gone_class`` and/or ``response_redirect_class``.
  61. .. attribute:: response_gone_class
  62. The :class:`~django.http.HttpResponse` class used when a
  63. :class:`~django.contrib.redirects.models.Redirect` is not
  64. found for the requested path or has a blank ``new_path``
  65. value.
  66. Defaults to :class:`~django.http.HttpResponseGone`.
  67. .. attribute:: response_redirect_class
  68. The :class:`~django.http.HttpResponse` class that handles the
  69. redirect.
  70. Defaults to :class:`~django.http.HttpResponsePermanentRedirect`.