Simple Redirect View for Django


I often find myself replacing an existing MVP based on static html with a Django app, or just needing to preserve some old URL scheme.

This is the code I use to do that:

from django.shortcuts import redirect

def redirect_view(request, redirectable, permanent=True):
  return redirect(redirectable)

Which can then be used like this:

from django.urls import path
from . import views

urlpatterns = [
    path("old-url/", views.redirect_view, {"redirectable": "new_view"}),
    path("some-thing/", views.redirect_view, {"redirectable": "some_thing_new", permanent=False}),
]

See also