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

Question:

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.

Asked By: pyDdev

||

Answers:

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.

Answered By: Stefano

I use Google Chrome.

When setting "permanent=True" to your Django Project, the permanent redirect is cached by Google Chrome whether or not disabling cache for Google Chrome.

And, just by removing "permanent=True" from your Django Project, you cannot remove the cache of the permanent redirect from Google Chrome.

So, you must delete the cache from Google Chrome by following this instruction.

Answered By: Kai – Kazuya Ito
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.