redirects.txt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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
  7. redirects in a database and handles the redirecting for you. It uses the HTTP
  8. response status code ``301 Moved Permanently`` by default.
  9. Installation
  10. ============
  11. To install the redirects app, follow these steps:
  12. #. Ensure that the ``django.contrib.sites`` framework
  13. :ref:`is installed <enabling-the-sites-framework>`.
  14. #. Add ``'django.contrib.redirects'`` to your :setting:`INSTALLED_APPS` setting.
  15. #. Add ``'django.contrib.redirects.middleware.RedirectFallbackMiddleware'``
  16. to your :setting:`MIDDLEWARE` setting.
  17. #. Run the command :djadmin:`manage.py migrate <migrate>`.
  18. How it works
  19. ============
  20. ``manage.py migrate`` creates a ``django_redirect`` table in your database. This
  21. is a lookup table with ``site_id``, ``old_path`` and ``new_path`` fields.
  22. The :class:`~django.contrib.redirects.middleware.RedirectFallbackMiddleware`
  23. does all of the work. Each time any Django application raises a 404
  24. error, this middleware checks the redirects database for the requested
  25. URL as a last resort. Specifically, it checks for a redirect with the
  26. given ``old_path`` with a site ID that corresponds to the
  27. :setting:`SITE_ID` setting.
  28. * If it finds a match, and ``new_path`` is not empty, it redirects to
  29. ``new_path`` using a 301 ("Moved Permanently") redirect. You can subclass
  30. :class:`~django.contrib.redirects.middleware.RedirectFallbackMiddleware`
  31. and set
  32. :attr:`~django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_redirect_class`
  33. to :class:`django.http.HttpResponseRedirect` to use a
  34. ``302 Moved Temporarily`` redirect instead.
  35. * If it finds a match, and ``new_path`` is empty, it sends a 410 ("Gone")
  36. HTTP header and empty (content-less) response.
  37. * If it doesn't find a match, the request continues to be processed as
  38. usual.
  39. The middleware only gets activated for 404s -- not for 500s or responses of any
  40. other status code.
  41. Note that the order of :setting:`MIDDLEWARE` matters. Generally, you can put
  42. :class:`~django.contrib.redirects.middleware.RedirectFallbackMiddleware` at the
  43. end of the list, because it's a last resort.
  44. For more on middleware, read the :doc:`middleware docs
  45. </topics/http/middleware>`.
  46. How to add, change and delete redirects
  47. =======================================
  48. Via the admin interface
  49. -----------------------
  50. If you've activated the automatic Django admin interface, you should see a
  51. "Redirects" section on the admin index page. Edit redirects as you edit any
  52. other object in the system.
  53. Via the Python API
  54. ------------------
  55. .. class:: models.Redirect
  56. Redirects are represented by a standard :doc:`Django model </topics/db/models>`,
  57. which lives in :source:`django/contrib/redirects/models.py`. You can access
  58. redirect objects via the :doc:`Django database API </topics/db/queries>`.
  59. For example:
  60. .. code-block:: pycon
  61. >>> from django.conf import settings
  62. >>> from django.contrib.redirects.models import Redirect
  63. >>> # Add a new redirect.
  64. >>> redirect = Redirect.objects.create(
  65. ... site_id=1,
  66. ... old_path="/contact-us/",
  67. ... new_path="/contact/",
  68. ... )
  69. >>> # Change a redirect.
  70. >>> redirect.new_path = "/contact-details/"
  71. >>> redirect.save()
  72. >>> redirect
  73. <Redirect: /contact-us/ ---> /contact-details/>
  74. >>> # Delete a redirect.
  75. >>> Redirect.objects.filter(site_id=1, old_path="/contact-us/").delete()
  76. (1, {'redirects.Redirect': 1})
  77. Middleware
  78. ==========
  79. .. class:: middleware.RedirectFallbackMiddleware
  80. You can change the :class:`~django.http.HttpResponse` classes used
  81. by the middleware by creating a subclass of
  82. :class:`~django.contrib.redirects.middleware.RedirectFallbackMiddleware`
  83. and overriding ``response_gone_class`` and/or ``response_redirect_class``.
  84. .. attribute:: response_gone_class
  85. The :class:`~django.http.HttpResponse` class used when a
  86. :class:`~django.contrib.redirects.models.Redirect` is not found for the
  87. requested path or has a blank ``new_path`` value.
  88. Defaults to :class:`~django.http.HttpResponseGone`.
  89. .. attribute:: response_redirect_class
  90. The :class:`~django.http.HttpResponse` class that handles the redirect.
  91. Defaults to :class:`~django.http.HttpResponsePermanentRedirect`.