I'm looking for a way to redirect any url that start with /D/
to the same URL with lowercased /d/
.
/D/<anything_including_url_params>
to
/d/<anything_including_url_params>
I literally only want to redirect urls that start with /D/
- not /DABC/
etc...
The suffix can also be empty, eg. /D/
> /d/
Is there a way to do that in Django? It is for a third-party app with urls included in projects urls.
The alternative is to use re_path
and change:
path("d/", include(...))
to
re_path(r"^[dD]/$", include(...))
but I'd rather do a redirect instead of this.
CodePudding user response:
Any reason why you can't use a RedirectView at all?
As per the docs:
https://docs.djangoproject.com/en/4.1/ref/class-based-views/base/#redirectview
You can register this as the literal /D/
but redirect to /d/
using the view name and passing on and args or kwargs.
CodePudding user response:
You can make a view that directs with:
# some_app/urls.py
from django.views.generic import RedirectView
# …
urlpatterns = [
path('d/', include(…)),
path(
'D/<path:path>',
RedirectView.as_view(
url='/d/%(path)s', query_string=True, permanent=True
),
),
]
Note that a redirect will however always result in a GET request, so even if the original request to D/something
is a POST request for example, it will make a GET request to d/something
.