Tasos Sangiotis

June 29, 2022

Django: Redirecting from urls.py

I made some changes and slashed a page on Mechbase.

Now it is possible some users have the page bookmarked or on an email and I wanted this to work but to not retain the url.

At the same time I did't want to create a view just for that.

There is a better way:

from django.views.generic import RedirectView

...

urlpatterns = [
    ...
    path(
        "measurementpoint/<uuid:pk>/history/",
        RedirectView.as_view(pattern_name="measurementpoint", permanent=True),
    ),
    ...
]


About Tasos Sangiotis

An electrical engineer at Arpedon with a broad range of interests from electrical, to mechanical & automation. Photoshooter, runner and a fan of good food & alcohol. You can also find me on Twitter, on LinkedIn, and on Instagram. If you choose to wander this wasteland do so with caution. Consider this your final warning.