{"id":75862,"date":"2020-01-20T12:23:47","date_gmt":"2020-01-20T06:53:47","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=75862"},"modified":"2024-04-12T14:17:41","modified_gmt":"2024-04-12T08:47:41","slug":"redirects","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/redirects\/","title":{"rendered":"Redirects"},"content":{"rendered":"<p>Django\u2019s redirects framework lets you manage redirects easily by storing them in a database and treating them as any other Django model object. For example, you can use the redirects framework to tell Django, \u201cRedirect any request to \/music\/ to \/sections\/arts\/music\/.\u201d This comes in handy when you need to move things around on your site; Web developers should do whatever is necessary to avoid broken links.<\/p>\n<p><strong>Using the Redirects Framework<\/strong> &#8211; To install the redirects application, follow these steps:<\/p>\n<ul>\n<li>Add &#8216;django.contrib.redirects&#8217; to your INSTALLED_APPS.<\/li>\n<li>Add &#8216;django.contrib.redirects.middleware.RedirectFallbackMiddleware&#8217; to your MIDDLEWARE_CLASSES<\/li>\n<li>Run the command py syncdb to install the single required table into your database.<\/li>\n<\/ul>\n<p>manage.py syncdb creates a django_redirect table in your database. This is a simple lookup table with site_id, old_path, and new_path fields. You can create redirects through either the Django admin interface or the Django database API.<\/p>\n<p>Once you\u2019ve created redirects, the RedirectFallbackMiddleware class does all of the work. Each time any Django application raises a 404 error, this middleware checks the redirects database for the requested URL as a last resort. Specifically, it checks for a redirect with the given old_path with a site ID that corresponds to the SITE_ID setting. (See the earlier section \u201cSites\u201d for more information on SITE_ID and the sites framework.) Then it follows these steps:<\/p>\n<ul>\n<li>If it finds a match, and new_path is not empty, it redirects to new_path.<\/li>\n<li>If it finds a match, and new_path is empty, it sends a 410 (\u201cGone\u201d) HTTP header and an empty (contentless) response.<\/li>\n<li>If it doesn\u2019t find a match, the request continues to be processed as usual.<\/li>\n<\/ul>\n<p>The middleware only gets activated for 404 errors \u2014 not for 500 errors or responses of any other status code. Note that the order of MIDDLEWARE_CLASSES matters. Generally, you can put<\/p>\n<p>RedirectFallbackMiddleware toward the end of the list, because it\u2019s a last resort.<\/p>\n<p><strong>Note<\/strong> &#8211; If you\u2019re using both the redirect and flatpage fallback middleware, consider which one (redirect or flatpage) you\u2019d like checked first. We suggest flatpages before redirects (thus putting the flatpage middleware before the redirect middleware), but you might feel differently.<\/p>\n<p><strong>Adding, Changing, and Deleting Redirects<\/strong> &#8211; You can add, change and delete redirects in two ways:<\/p>\n<p><u>Via the Admin Interface<\/u> &#8211; If you\u2019ve activated the automatic Django admin interface, you should see a \u201cRedirects\u201d section on the admin index page. Edit redirects as you would edit any other object in the system.<\/p>\n<p><u>Via the Python API<\/u> &#8211; Redirects are represented by a standard Django model that lives in django\/contrib\/redirects\/models.py. Hence, you can access redirect objects via the Django database API, for example:<\/p>\n<blockquote><p>&gt;&gt; from django.contrib.redirects.models import Redirect<\/p>\n<p>&gt;&gt; from django.contrib.sites.models import Site<\/p>\n<p>&gt;&gt; red = Redirect(&#8230;&nbsp;&nbsp;&nbsp;&nbsp; site=Site.objects.get(id=1)<\/p><\/blockquote>\n<p>,&#8230;&nbsp;&nbsp;&nbsp;&nbsp; old_path=&#8217;\/music\/&#8217;<\/p>\n<p>,&#8230;&nbsp;&nbsp;&nbsp;&nbsp; new_path=&#8217;\/sections\/arts\/music\/&#8217;<\/p>\n<p>,&#8230; )<\/p>\n<blockquote><p>&gt;&gt; red.save()<\/p>\n<p>&gt;&gt; Redirect.objects.get(old_path=&#8217;\/music\/&#8217;)<\/p><\/blockquote>\n<p>&lt;Redirect: \/music\/ &#8212;&gt; \/sections\/arts\/music\/&gt;<\/p>\n\n\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/certified-django-developer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Back to Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Django\u2019s redirects framework lets you manage redirects easily by storing them in a database and treating them as any other Django model object. For example, you can use the redirects framework to tell Django, \u201cRedirect any request to \/music\/ to \/sections\/arts\/music\/.\u201d This comes in handy when you need to move things around on your site;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8655],"tags":[8842],"class_list":["post-75862","page","type-page","status-publish","hentry","category-django-web-development","tag-redirects"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Redirects - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/redirects\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Redirects - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Django\u2019s redirects framework lets you manage redirects easily by storing them in a database and treating them as any other Django model object. For example, you can use the redirects framework to tell Django, \u201cRedirect any request to \/music\/ to \/sections\/arts\/music\/.\u201d This comes in handy when you need to move things around on your site;...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/redirects\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:47:41+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/redirects\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/redirects\/\",\"name\":\"Redirects - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-20T06:53:47+00:00\",\"dateModified\":\"2024-04-12T08:47:41+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/redirects\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/redirects\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/redirects\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Redirects\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Redirects - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/redirects\/","og_locale":"en_US","og_type":"article","og_title":"Redirects - Tutorial","og_description":"Django\u2019s redirects framework lets you manage redirects easily by storing them in a database and treating them as any other Django model object. For example, you can use the redirects framework to tell Django, \u201cRedirect any request to \/music\/ to \/sections\/arts\/music\/.\u201d This comes in handy when you need to move things around on your site;...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/redirects\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:47:41+00:00","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/redirects\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/redirects\/","name":"Redirects - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-20T06:53:47+00:00","dateModified":"2024-04-12T08:47:41+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/redirects\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/redirects\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/redirects\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Redirects"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=75862"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75862\/revisions"}],"predecessor-version":[{"id":83405,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75862\/revisions\/83405"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=75862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=75862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=75862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}