0
0
Djangoframework~20 mins

Login view and template in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django Login Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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')
ARedirects to 'home' on GET request
BRenders 'login.html' template without any context on GET
CRenders 'home.html' template on GET
DRaises a 404 error on GET request
Attempts:
2 left
💡 Hint
Check what happens when the request method is not POST.
📝 Syntax
intermediate
2: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>
A{% csrf_token %}
B{{ csrf_token }}
C<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
D<csrf_token />
Attempts:
2 left
💡 Hint
Django uses a special template tag for CSRF tokens.
state_output
advanced
2: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')
A'Invalid credentials'
BKeyError because 'error' is missing
CEmpty string ''
DNone
Attempts:
2 left
💡 Hint
Look at the else block inside the POST method.
🔧 Debug
advanced
2: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')
Aredirect called without parentheses
Blogin function called with wrong number of arguments
Cauthenticate missing 'request' argument causes TypeError
Drequest.POST['username'] raises KeyError
Attempts:
2 left
💡 Hint
Check the signature of authenticate in Django 4+.
🧠 Conceptual
expert
2: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?
AIt logs the user out and clears the session
BIt creates a new user account in the database
CIt redirects the user to the login page
DIt sets the user ID in the session to mark the user as authenticated
Attempts:
2 left
💡 Hint
Think about what happens after a successful login.