[Django]-Django redirect preserving subpath

4👍

According to the docs something like this should work, you can capture groups from the path and pass them to the url

The given URL may contain dictionary-style string formatting, which will be interpolated against the parameters captured in the URL. Because keyword interpolation is always done (even if no arguments are passed in), any “%” characters in the URL must be written as “%%” so that Python will convert them to a single percent sign on output.

url(r'^django-admin/(?P<rest>.*)', RedirectView.as_view(url='/admin/%(rest)s', permanent=True)

This website is particularly useful for figuring out how the built-in class-based views work http://ccbv.co.uk/projects/Django/2.2/django.views.generic.base/RedirectView/#get_redirect_url

Leave a comment