Django Redirect

In Django, redirects are used to send users to a different URL from the one they initially requested. Django provides utilities for managing redirects easily in both views and templates.

How Redirects Work

When a redirect is triggered, Django sends an HTTP response with a status code (usually 302 Found or 301 Moved Permanently) and the new target URL. The browser automatically navigates to the new location.

There are many ways to perform Redirect

1. Using redirect()

Django’s redirect() shortcut simplifies redirection by handling URLs, named routes, and model objects.

Example:


from django.shortcuts import redirect

def my_view(request):
    return redirect('/new-url/')  # Redirect to a specific URL

Redirecting to a named URL pattern:


def my_view(request):
    return redirect('home')  # 'home' is the name of a URL pattern

Redirecting to an object’s URL:


def my_view(request, pk):
    from myapp.models import MyModel
    obj = MyModel.objects.get(pk=pk)
    return redirect(obj)  # Redirects to the URL of the object

2. Using HttpResponseRedirect

The HttpResponseRedirect class allows manual control over redirection.

Example:


from django.http import HttpResponseRedirect

def my_view(request):
    return HttpResponseRedirect('/new-url/')

3. Redirecting After Form Submission

Redirects are often used after successful form submissions to prevent duplicate form submissions when refreshing the page.

Example:


def submit_form(request):
    if request.method == "POST":
        # Process the form data
        return redirect('success-page')
    return render(request, 'form.html')

4. Permanent Redirects

To create a permanent redirect (301 Moved Permanently), use the HttpResponsePermanentRedirect class.

Example


from django.http import HttpResponsePermanentRedirect

def old_view(request):
    return HttpResponsePermanentRedirect('/new-url/')

Redirects in URL Configuration

Django’s URL routing system allows simple redirects using django.views.generic.RedirectView.

Example:


from django.urls import path
from django.views.generic import RedirectView

urlpatterns = [
    path('old-url/', RedirectView.as_view(url='/new-url/', permanent=True)),
]

Permanent vs Temporary Redirect:

Use permanent=True for a 301 redirect.

Use permanent=False for a 302 redirect (default).

Django Redirect – Interview Questions

Q 1: What is redirect in Django?
Ans: Redirect sends the user to a different URL.
Q 2: How do you redirect in Django views?
Ans: Using redirect() function.
Q 3: Difference between redirect and render?
Ans: Redirect changes URL, render returns template.
Q 4: Can redirect be used after form submission?
Ans: Yes, to prevent duplicate submissions.
Q 5: What is permanent redirect?
Ans: Redirect with HTTP status code 301.

Django Redirect – Objective Questions (MCQs)

Q1. Which function is used for redirecting users?






Q2. Redirects are part of which Django module?






Q3. Redirects are used to send users to:






Q4. Which HTTP status code is commonly used for redirects?






Q5. redirect() can redirect using:






Related Django Redirect Topics