In my URLs.py, I’m setting my URL like this
url(r'^mobile/$', views.mobile, name='mobile')
Navigating to localhost:8000/mobile works as expected but if typed in the URL: localhost:8000/mobile/hello/world/234234hjhf8sc3 it should redirect to localhost:8000/mobile
however this does not happen because my regex is incorrect (I’m not sure)
How could I do this/what is the correct regex to do this?
Another example would be localhost:8000/mobile/send/help/as792lp should redirect to
localhost:8000/mobile
Advertisement
Answer
You can work with a RedirectView [Django-doc] to redirect any URL tat starts with a mobile/ to the mobile view:
from django.urls import reverse_lazy
from django.views.generic.base import RedirectView
url(r'^mobile/$', views.mobile, name='mobile')
url(r'^mobile/.+$', RedirectView.as_view(url=reverse_lazy('mobile')))Note: As of django-3.1,
url(…)[Django-doc] is deprecated in favor ofre_path(…)[Django-doc]. Furthermore a new syntax for paths has been introduced with path converters: you usepath(…)[Django-doc] for that.