Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirection in Django URLs

How do I redirect all URLs starting from a particular pattern to another?

Description:

I want to be able to redirect as follows:

/pattern1/step1/ to /pattern2/step1/
/pattern1/step2/ to /pattern2/step2/
/pattern1/continue/ to /pattern2/continue/

What is the easiest way of doing this in Django URL patterns?

like image 447
Alagappan Ramu Avatar asked Jul 13 '26 17:07

Alagappan Ramu


1 Answers

RedirectView works. Capture the remainder of the path with a named kwarg. It will be passed into RedirectView.get_redirect_url, so you can interpolate it into the url you provide.

url(r'^pattern1/(?P<url>.+)$', RedirectView.as_view(url="/pattern2/%(url)s")),
#                    ^                                                ^
#                    | this url                         appears here  |
like image 180
dokkaebi Avatar answered Jul 16 '26 09:07

dokkaebi