0
0
Djangoframework~10 mins

Redirects with redirect function in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Redirects with redirect function
User sends HTTP request
View function starts
Call redirect() with URL or view name
redirect() creates HttpResponseRedirect
Return redirect response to browser
Browser receives redirect status
Browser requests new URL
New view processes request
This flow shows how a Django view uses redirect() to send a browser to a new URL by returning a special response that tells the browser to load another page.
Execution Sample
Django
from django.shortcuts import redirect

def my_view(request):
    return redirect('/home/')
This code sends the user to the '/home/' URL by returning a redirect response.
Execution Table
StepActionInput/StateOutput/Result
1User sends request to my_viewRequest to /start/Request received by my_view
2my_view calls redirect('/home/')URL '/home/'HttpResponseRedirect object created with status 302 and Location '/home/'
3my_view returns redirect responseHttpResponseRedirect objectResponse sent to browser with status 302
4Browser receives 302 responseStatus 302, Location '/home/'Browser prepares new request to '/home/'
5Browser sends new requestRequest to '/home/'New request handled by Django view for '/home/'
💡 Redirect response sent, browser follows Location header to new URL
Variable Tracker
VariableStartAfter redirect callAfter return
requestHttpRequest to /start/HttpRequest to /start/HttpRequest to /start/
responseNoneHttpResponseRedirect(status=302, Location='/home/')HttpResponseRedirect(status=302, Location='/home/')
Key Moments - 2 Insights
Why does the browser make a new request after redirect?
Because the redirect() returns a response with status 302 and a Location header. The browser sees this and automatically requests the new URL, as shown in execution_table step 4 and 5.
Is redirect() sending the user directly to the new page?
No, redirect() returns a special response telling the browser to go to a new URL. The browser then makes a new request. This is shown in steps 3 (response sent) and 4 (browser receives redirect).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what HTTP status code does redirect() set in the response?
A200 OK
B404 Not Found
C302 Found
D500 Internal Server Error
💡 Hint
Check step 2 in the execution_table where HttpResponseRedirect is created
At which step does the browser decide to request the new URL?
AStep 4
BStep 3
CStep 1
DStep 5
💡 Hint
Look at the execution_table step where browser receives the 302 response
If redirect() was called with a view name instead of a URL, what would change in the execution?
AThe response status code would change to 404
BThe redirect response would have a different Location URL
CThe browser would not follow the redirect
DThe request object would change
💡 Hint
redirect() always returns a 302 response but the Location header depends on the argument
Concept Snapshot
redirect() in Django returns an HttpResponseRedirect
It sends a 302 status with a Location header
Browser receives this and requests the new URL
Use redirect() to send users to another page easily
Can pass URL string or view name to redirect()
Full Transcript
In Django, the redirect function helps send users to a different page. When a user sends a request, the view can call redirect() with a URL or view name. This creates a special response with status 302 and a Location header telling the browser where to go next. The browser then makes a new request to that URL. This process involves the view returning the redirect response, the browser receiving it, and then requesting the new page. This is a common way to move users after actions like login or form submission.