{"title":"The redirects app","version":"2.0","locale":"es","docname":"ref/contrib/redirects","url":"/es/2.0/ref/contrib/redirects/","canonical":"https://djangodocs.dev/es/2.0/ref/contrib/redirects/","summary":"Django comes with an optional redirects application. It lets you store simple redirects in a database and handles the redirecting for you. It uses the HTTP response…","html":"<span id=\"the-redirects-app\"></span><h1>The redirects app<a class=\"heading-anchor\" href=\"#module-django.contrib.redirects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Django comes with an optional redirects application. It lets you store simple\nredirects in a database and handles the redirecting for you. It uses the HTTP\nresponse status code <code class=\"docutils literal notranslate\">301 Moved Permanently</code> by default.</p>\n<section id=\"installation\">\n<h2>Installation<a class=\"heading-anchor\" href=\"#installation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>To install the redirects app, follow these steps:</p>\n<ol class=\"arabic simple\">\n<li><p>Ensure that the <code class=\"docutils literal notranslate\">django.contrib.sites</code> framework\n<a class=\"reference internal\" href=\"/es/2.0/ref/contrib/sites/#enabling-the-sites-framework\"><span class=\"std std-ref\">is installed</span></a>.</p></li>\n<li><p>Add <code class=\"docutils literal notranslate\">'django.contrib.redirects'</code> to your <a class=\"reference internal\" href=\"/es/2.0/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> setting.</p></li>\n<li><p>Add <code class=\"docutils literal notranslate\">'django.contrib.redirects.middleware.RedirectFallbackMiddleware'</code>\nto your <a class=\"reference internal\" href=\"/es/2.0/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\">MIDDLEWARE</code></a> setting.</p></li>\n<li><p>Run the command <a class=\"reference internal\" href=\"/es/2.0/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">manage.py migrate</code></a>.</p></li>\n</ol>\n</section>\n<section id=\"how-it-works\">\n<h2>How it works<a class=\"heading-anchor\" href=\"#how-it-works\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><code class=\"docutils literal notranslate\">manage.py migrate</code> creates a <code class=\"docutils literal notranslate\">django_redirect</code> table in your database. This\nis a simple lookup table with <code class=\"docutils literal notranslate\">site_id</code>, <code class=\"docutils literal notranslate\">old_path</code> and <code class=\"docutils literal notranslate\">new_path</code> fields.</p>\n<p>The <a class=\"reference internal\" href=\"#django.contrib.redirects.middleware.RedirectFallbackMiddleware\" title=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">RedirectFallbackMiddleware</code></a>\ndoes all of the work. Each time any Django application raises a 404\nerror, this middleware checks the redirects database for the requested\nURL as a last resort. Specifically, it checks for a redirect with the\ngiven <code class=\"docutils literal notranslate\">old_path</code> with a site ID that corresponds to the\n<a class=\"reference internal\" href=\"/es/2.0/ref/settings/#std-setting-SITE_ID\"><code class=\"xref std std-setting docutils literal notranslate\">SITE_ID</code></a> setting.</p>\n<ul class=\"simple\">\n<li><p>If it finds a match, and <code class=\"docutils literal notranslate\">new_path</code> is not empty, it redirects to\n<code class=\"docutils literal notranslate\">new_path</code> using a 301 («Moved Permanently») redirect. You can subclass\n<a class=\"reference internal\" href=\"#django.contrib.redirects.middleware.RedirectFallbackMiddleware\" title=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">RedirectFallbackMiddleware</code></a>\nand set\n<a class=\"reference internal\" href=\"#django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_redirect_class\" title=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_redirect_class\"><code class=\"xref py py-attr docutils literal notranslate\">response_redirect_class</code></a>\nto <a class=\"reference internal\" href=\"/es/2.0/ref/request-response/#django.http.HttpResponseRedirect\" title=\"django.http.HttpResponseRedirect\"><code class=\"xref py py-class docutils literal notranslate\">django.http.HttpResponseRedirect</code></a> to use a\n<code class=\"docutils literal notranslate\">302 Moved Temporarily</code> redirect instead.</p></li>\n<li><p>If it finds a match, and <code class=\"docutils literal notranslate\">new_path</code> is empty, it sends a 410 («Gone»)\nHTTP header and empty (content-less) response.</p></li>\n<li><p>If it doesn’t find a match, the request continues to be processed as\nusual.</p></li>\n</ul>\n<p>The middleware only gets activated for 404s – not for 500s or responses of any\nother status code.</p>\n<p>Note that the order of <a class=\"reference internal\" href=\"/es/2.0/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\">MIDDLEWARE</code></a> matters. Generally, you can put\n<a class=\"reference internal\" href=\"#django.contrib.redirects.middleware.RedirectFallbackMiddleware\" title=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">RedirectFallbackMiddleware</code></a> at the\nend of the list, because it’s a last resort.</p>\n<p>For more on middleware, read the <a class=\"reference internal\" href=\"/es/2.0/topics/http/middleware/\"><span class=\"doc\">middleware docs</span></a>.</p>\n</section>\n<section id=\"how-to-add-change-and-delete-redirects\">\n<h2>How to add, change and delete redirects<a class=\"heading-anchor\" href=\"#how-to-add-change-and-delete-redirects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"via-the-admin-interface\">\n<h3>Via the admin interface<a class=\"heading-anchor\" href=\"#via-the-admin-interface\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you’ve activated the automatic Django admin interface, you should see a\n«Redirects» section on the admin index page. Edit redirects as you edit any\nother object in the system.</p>\n</section>\n<section id=\"via-the-python-api\">\n<h3>Via the Python API<a class=\"heading-anchor\" href=\"#via-the-python-api\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.redirects.models.Redirect\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-prename descclassname\">models.</span><span class=\"sig-name descname\">Redirect</span><a class=\"heading-anchor\" href=\"#django.contrib.redirects.models.Redirect\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Redirects are represented by a standard <a class=\"reference internal\" href=\"/es/2.0/topics/db/models/\"><span class=\"doc\">Django model</span></a>,\nwhich lives in <a class=\"reference external\" href=\"https://github.com/django/django/blob/master/django/contrib/redirects/models.py\">django/contrib/redirects/models.py</a>. You can access redirect\nobjects via the <a class=\"reference internal\" href=\"/es/2.0/topics/db/queries/\"><span class=\"doc\">Django database API</span></a>.</p>\n</dd></dl>\n\n</section>\n</section>\n<section id=\"middleware\">\n<h2>Middleware<a class=\"heading-anchor\" href=\"#middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-prename descclassname\">middleware.</span><span class=\"sig-name descname\">RedirectFallbackMiddleware</span><a class=\"heading-anchor\" href=\"#django.contrib.redirects.middleware.RedirectFallbackMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>You can change the <a class=\"reference internal\" href=\"/es/2.0/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\">HttpResponse</code></a> classes used\nby the middleware by creating a subclass of\n<a class=\"reference internal\" href=\"#django.contrib.redirects.middleware.RedirectFallbackMiddleware\" title=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">RedirectFallbackMiddleware</code></a>\nand overriding <code class=\"docutils literal notranslate\">response_gone_class</code> and/or <code class=\"docutils literal notranslate\">response_redirect_class</code>.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_gone_class\">\n<span class=\"sig-name descname\">response_gone_class</span><a class=\"heading-anchor\" href=\"#django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_gone_class\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The <a class=\"reference internal\" href=\"/es/2.0/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\">HttpResponse</code></a> class used when a\n<a class=\"reference internal\" href=\"#django.contrib.redirects.models.Redirect\" title=\"django.contrib.redirects.models.Redirect\"><code class=\"xref py py-class docutils literal notranslate\">Redirect</code></a> is not found for the\nrequested path or has a blank <code class=\"docutils literal notranslate\">new_path</code> value.</p>\n<p>Defaults to <a class=\"reference internal\" href=\"/es/2.0/ref/request-response/#django.http.HttpResponseGone\" title=\"django.http.HttpResponseGone\"><code class=\"xref py py-class docutils literal notranslate\">HttpResponseGone</code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_redirect_class\">\n<span class=\"sig-name descname\">response_redirect_class</span><a class=\"heading-anchor\" href=\"#django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_redirect_class\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The <a class=\"reference internal\" href=\"/es/2.0/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\">HttpResponse</code></a> class that handles the redirect.</p>\n<p>Defaults to <a class=\"reference internal\" href=\"/es/2.0/ref/request-response/#django.http.HttpResponsePermanentRedirect\" title=\"django.http.HttpResponsePermanentRedirect\"><code class=\"xref py py-class docutils literal notranslate\">HttpResponsePermanentRedirect</code></a>.</p>\n</dd></dl>\n\n</dd></dl>\n\n</section>","rootId":"module-django.contrib.redirects","toc":[{"title":"Installation","anchor":"installation","children":[]},{"title":"How it works","anchor":"how-it-works","children":[]},{"title":"How to add, change and delete redirects","anchor":"how-to-add-change-and-delete-redirects","children":[{"title":"Via the admin interface","anchor":"via-the-admin-interface","children":[]},{"title":"Via the Python API","anchor":"via-the-python-api","children":[]}]},{"title":"Middleware","anchor":"middleware","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/es/2.0/ref/"},{"docname":"ref/contrib/index","title":"contrib packages","url":"/es/2.0/ref/contrib/"}],"prev":{"docname":"ref/contrib/postgres/validators","title":"Validators","url":"/es/2.0/ref/contrib/postgres/validators/"},"next":{"docname":"ref/contrib/sitemaps","title":"The sitemap framework","url":"/es/2.0/ref/contrib/sitemaps/"},"formats":{"html":"/es/2.0/ref/contrib/redirects/","markdown":"/es/2.0/ref/contrib/redirects.md","json":"/es/2.0/ref/contrib/redirects.json"},"source":"https://github.com/django/django/blob/stable/2.0.x/docs/ref/contrib/redirects.txt","official":"https://docs.djangoproject.com/es/2.0/ref/contrib/redirects/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}