Skip to content
Advertisement

Django: Stop redirecting URL to another page after setting it with permanent=True

For some reason in my Django app, I used a redirect path as in my urls.py file, such that whenever someone visits example.com they would be redirected to example.com/blog

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls', namespace='blog')),
    path('', lambda request: redirect('blog/', permanent=True)),
]

Now I have constructed my site fully and added views for example.com. In order to visit that page I removed the line that redirects path from my code base so that it no longer redirects to /blog whenever I try to visit example.com.

path('', lambda request: redirect('blog/', permanent=True))

But the problem is that I’m still being redirected to /blog even after removing that above line.

The same problem persist to me even in entirely new django project. i.e. Even if I run another django project i can’t visit “/” path it keeps being redirected to “/blog/”. I think it is happening due to some thing related with permanent=True. Any help would be appreciated in reversing that effect.

Thanks in advance.

Advertisement

Answer

The flag permanent=True implies that your webserver returns a 301 - Permanent Redirect when someone tries to access the index page. You probably should have avoided using it and let the webserver return a 302 - Temporary Redirect.

Status 301 means that the resource (page) is moved permanently to a new location. The client (browser) should not attempt to request the original location but use the new location from now on.

Status 302 means that the resource is temporarily located somewhere else, and the client (browser) should continue requesting the original URL.

Try not to remove the whole line but adjust it like as follow

path('', views.index, name='index')

Try also to test it in incognito mode, or disabling the cache.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement