Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using the login_required Decorator in Django
📖 Scenario: You are building a simple Django web app where some pages should only be seen by users who have logged in. To protect these pages, you will use Django's login_required decorator.
🎯 Goal: Learn how to use the login_required decorator to restrict access to a view so that only logged-in users can see it.
📋 What You'll Learn
Create a Django view function named dashboard that returns a simple HTTP response.
Import and use the login_required decorator from django.contrib.auth.decorators.
Apply the login_required decorator to the dashboard view.
Add a URL pattern for the dashboard view in urls.py.
💡 Why This Matters
🌍 Real World
Many websites have pages that only logged-in users should see, like user dashboards, profiles, or settings. Protecting these pages is important for privacy and security.
💼 Career
Understanding how to restrict access to parts of a web app is a key skill for web developers working with Django or any web framework.
Progress0 / 4 steps
1
Create the dashboard view function
In your Django app's views.py, create a function called dashboard that takes a request parameter and returns an HttpResponse with the text "Welcome to your dashboard!". Import HttpResponse from django.http.
Django
Hint
Remember to import HttpResponse and define a function named dashboard that returns it with the welcome text.
2
Import the login_required decorator
In views.py, add an import statement to import login_required from django.contrib.auth.decorators.
Django
Hint
Use from django.contrib.auth.decorators import login_required to import the decorator.
3
Apply the login_required decorator to the dashboard view
Add the @login_required decorator above the dashboard function definition in views.py.
Django
Hint
Place @login_required directly above the dashboard function.
4
Add a URL pattern for the dashboard view
In your app's urls.py, import the dashboard view and add a URL pattern for path dashboard/ that points to the dashboard view. Import path from django.urls.
Django
Hint
Use path('dashboard/', dashboard, name='dashboard') inside urlpatterns.
Practice
(1/5)
1. What is the main purpose of the @login_required decorator in Django?
easy
A. To restrict access to a view only to logged-in users
B. To automatically log out users after a timeout
C. To display a custom error message on login failure
D. To register a new user in the system
Solution
Step 1: Understand the role of @login_required
This decorator is used to protect views so only authenticated users can access them.
Step 2: Compare options with the decorator's function
Only To restrict access to a view only to logged-in users correctly describes restricting access to logged-in users.
Final Answer:
To restrict access to a view only to logged-in users -> Option A
Quick Check:
login_required restricts access = D [OK]
Hint: Remember: login_required means login needed to see page [OK]
Common Mistakes:
Thinking it logs out users automatically
Confusing it with user registration
Assuming it shows error messages
2. Which of the following is the correct way to apply the @login_required decorator to a Django view function named dashboard?
easy
A. def login_required(dashboard):
B. @login_required\ndef dashboard(request):
C. dashboard = login_required(dashboard(request))
D. login_required @dashboard(request):
Solution
Step 1: Recall the syntax for decorators in Python
Decorators are placed above the function with an @ symbol, like @login_required.
Step 2: Check which option uses this syntax correctly
@login_required\ndef dashboard(request): correctly places @login_required above the function definition.
Final Answer:
@login_required\ndef dashboard(request): -> Option B
Quick Check:
Decorator syntax uses @ above function = A [OK]
Hint: Decorator always goes above function with @ [OK]
Common Mistakes:
Trying to call decorator like a function without @
Placing decorator after function definition
Using invalid syntax like 'login_required @dashboard'
3. Given this Django view code snippet, what happens when an anonymous user tries to access /profile/?