Challenge - 5 Problems
Login Required Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when an anonymous user accesses a view with @login_required?
Consider a Django view decorated with
@login_required. What is the behavior when an anonymous (not logged in) user tries to access this view?Django
from django.contrib.auth.decorators import login_required from django.http import HttpResponse @login_required def secret_view(request): return HttpResponse('Secret content')
Attempts:
2 left
💡 Hint
Think about what Django does to protect views from anonymous users.
✗ Incorrect
The @login_required decorator checks if the user is authenticated. If not, it redirects them to the login page specified by LOGIN_URL.
📝 Syntax
intermediate2:00remaining
Which code correctly applies @login_required to a class-based view?
You want to protect a Django class-based view using the @login_required decorator. Which option correctly applies it?
Django
from django.contrib.auth.decorators import login_required from django.utils.decorators import method_decorator from django.views import View from django.http import HttpResponse class MyView(View): def get(self, request): return HttpResponse('Hello')
Attempts:
2 left
💡 Hint
Remember that @login_required is a function decorator, and class-based views need a special approach.
✗ Incorrect
For class-based views, you use method_decorator on the dispatch method or with the name argument to apply @login_required properly.
❓ state_output
advanced2:00remaining
What is the value of request.user after @login_required redirects?
In a Django view decorated with @login_required, if the user is not logged in and gets redirected to the login page, what is the value of
request.user in the login view?Django
from django.contrib.auth.decorators import login_required from django.http import HttpResponse @login_required def secret_view(request): return HttpResponse(f'User: {request.user}')
Attempts:
2 left
💡 Hint
Think about what Django sets for unauthenticated users.
✗ Incorrect
Django sets request.user to an AnonymousUser instance for users who are not logged in.
🔧 Debug
advanced2:00remaining
Why does @login_required not redirect in this code?
This Django view uses @login_required but anonymous users can still access it. Why?
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
@login_required
def my_view():
return HttpResponse('Hello')Attempts:
2 left
💡 Hint
Check the function signature carefully.
✗ Incorrect
The view function must accept the request argument. Without it, Django cannot pass the request, so the decorator does not work properly.
🧠 Conceptual
expert2:00remaining
How does @login_required handle the 'next' parameter in redirects?
When @login_required redirects an anonymous user to the login page, it appends a 'next' parameter to the URL. What is the purpose of this 'next' parameter?
Attempts:
2 left
💡 Hint
Think about user experience after login.
✗ Incorrect
The 'next' parameter holds the original URL the user wanted to visit, so after login, Django can redirect them there.