- Within your views.py file, import the RedirectView from django.views.generic.base module:
from django.views.generic import RedirectView
- Create a RedirectView subclass and define the pattern with the URL you want to redirect to, using the pattern_name attribute:
class MyRedirectView(RedirectView):
pattern_name = 'target_url_name'
- Add the URL pattern to the urls.py file, specifying the path and the associated RedirectView subclass:
from django.urls import path
from .views import MyRedirectView
urlpatterns = [
path('old-url/', MyRedirectView.as_view(), name='old_url_name'),
]