Return a redirect response in Django

from django.http import HttpResponseRedirect

def my_view(request):
    # your logic here
    return HttpResponseRedirect('/another-page/')

Or, if you want to redirect to a URL resolved by a named URL pattern, you’d typically use the reverse function to dynamically build the URL:

# urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('home/', views.home, name='home'),
    path('profile/<str:username>/', views.profile, name='profile'),
]
# views.py

from django.http import HttpResponseRedirect
from django.urls import reverse

def my_view(request):
    # Example logic here
    # Suppose we determine the username as follows:
    username = 'john_doe'
    
    # Using reverse to dynamically generate the URL
    url = reverse('profile', args=[username])
    
    # Redirecting to the dynamically generated URL
    return HttpResponseRedirect(url)

def home(request):
    # Logic for home view
    pass

def profile(request, username):
    # Logic for profile view
    return HttpResponse(f"Profile page of {username}")