Sometimes, we want to add URL Redirect with Python Django.
In this article, we’ll look at how to add URL Redirect with Python Django.
How to add URL Redirect with Python Django?
To add URL Redirect with Python Django, we can use the RedirectView.as_view
method.
For instance, we write
from django.urls import re_path
re_path(r'^.*$', RedirectView.as_view(url='home', permanent=False), name='index')
to create the /
route that redirects to the home
URL.
We cann RedirectView.as_view
with some arguments to redirect /
to home
.
We set the permanent
to False
to do a 302 redirect.
And we can set permanent
to True
to do a 301 redirect.
Conclusion
To add URL Redirect with Python Django, we can use the RedirectView.as_view
method.