Challenge - 5 Problems
Django Login Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Django login view render on GET request?
Consider this Django view code for login. What template does it render when accessed with a GET request?
Django
from django.shortcuts import render, redirect from django.contrib.auth import authenticate, login def login_view(request): if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') user = authenticate(request, username=username, password=password) if user is not None: login(request, user) return redirect('home') else: return render(request, 'login.html', {'error': 'Invalid credentials'}) else: return render(request, 'login.html')
Attempts:
2 left
💡 Hint
Check what happens when the request method is not POST.
✗ Incorrect
The view checks if the request method is POST to process login. For GET requests, it renders 'login.html' without extra context.
📝 Syntax
intermediate2:00remaining
Which option correctly includes CSRF token in Django login template?
In Django templates, to protect POST forms from CSRF attacks, which snippet correctly includes the CSRF token inside the form?
Django
<form method="post" action=""> <!-- CSRF token here --> <input type="text" name="username" /> <input type="password" name="password" /> <button type="submit">Login</button> </form>
Attempts:
2 left
💡 Hint
Django uses a special template tag for CSRF tokens.
✗ Incorrect
Django requires the template tag {% csrf_token %} inside POST forms to add the CSRF token input automatically.
❓ state_output
advanced2:00remaining
What is the value of 'error' in context after failed login POST?
Given this login view, what will be the value of 'error' in the context passed to the template after a failed login attempt?
Django
def login_view(request): if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') user = authenticate(request, username=username, password=password) if user is not None: login(request, user) return redirect('home') else: return render(request, 'login.html', {'error': 'Invalid credentials'}) else: return render(request, 'login.html')
Attempts:
2 left
💡 Hint
Look at the else block inside the POST method.
✗ Incorrect
When authentication fails, the view renders 'login.html' with context {'error': 'Invalid credentials'}.
🔧 Debug
advanced2:00remaining
Why does this login view raise a TypeError?
Identify the cause of the TypeError in this Django login view snippet.
Django
def login_view(request): if request.method == 'POST': username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user: login(request, user) return redirect('home') else: return render(request, 'login.html', {'error': 'Invalid credentials'}) return render(request, 'login.html')
Attempts:
2 left
💡 Hint
Check the signature of authenticate in Django 4+.
✗ Incorrect
In Django 4+, authenticate requires the request argument. Omitting it causes a TypeError.
🧠 Conceptual
expert2:00remaining
Which option best describes the purpose of 'login' function in Django views?
In Django, what is the main effect of calling the 'login(request, user)' function inside a view?
Attempts:
2 left
💡 Hint
Think about what happens after a successful login.
✗ Incorrect
The login function saves the user's ID in the session so Django knows the user is authenticated on future requests.