Categories
Python Answers

How to add URL Redirect with Python Django?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *