0
0
Djangoframework~10 mins

login_required decorator in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - login_required decorator
User sends request to view
login_required checks if user is authenticated
Allow access
View executes
The login_required decorator checks if the user is logged in before allowing access to a view. If not logged in, it redirects to the login page.
Execution Sample
Django
@login_required
def dashboard(request):
    return HttpResponse('Welcome to your dashboard')
This code protects the dashboard view so only logged-in users can see it.
Execution Table
StepActionUser Authenticated?ResultNext Step
1User sends request to dashboard viewUnknownCheck authenticationlogin_required decorator runs
2login_required checks user authenticationYesAllow accessdashboard view executes
3dashboard view returns responseYesHttpResponse with welcome messageResponse sent to user
4User sends request to dashboard viewUnknownCheck authenticationlogin_required decorator runs
5login_required checks user authenticationNoRedirect to login pageRedirect response sent to user
💡 Execution stops after sending HttpResponse or redirecting to login page
Variable Tracker
VariableStartAfter Step 2After Step 3 or 5
user.is_authenticatedUnknownTrue or FalseTrue or False
responseNoneNoneHttpResponse or HttpResponseRedirect
Key Moments - 2 Insights
Why does the user get redirected instead of seeing the dashboard?
Because login_required found user.is_authenticated is False at step 5, so it sends a redirect to the login page instead of running the view.
What happens if the user is authenticated?
At step 2, login_required sees user.is_authenticated is True, so it lets the dashboard view run and return the welcome message (step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 5?
ADashboard view returns welcome message
BAuthentication is checked again
CUser is redirected to login page
DUser is logged out
💡 Hint
See row with Step 5 in execution_table where user is not authenticated
At which step does the dashboard view actually run?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Look at execution_table row where dashboard view returns response
If the user is authenticated, what will the response variable hold after step 3?
AHttpResponse with welcome message
BNone
CRedirect to login page
DError message
💡 Hint
Check variable_tracker for response after step 3
Concept Snapshot
login_required decorator syntax:
@login_required
def view(request):
  # code

Behavior:
- Checks if user is logged in
- If yes, runs view
- If no, redirects to login page

Use to protect views needing login
Full Transcript
The login_required decorator in Django protects views by checking if the user is logged in. When a user requests a protected view, login_required first checks user.is_authenticated. If True, the view runs and returns its response. If False, the user is redirected to the login page. This ensures only logged-in users can access certain pages. The execution table shows these steps clearly: request arrives, authentication checked, then either view runs or redirect happens. Variables like user.is_authenticated and response change accordingly during execution.