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
Login view and template
📖 Scenario: You are building a simple login page for a website. Users will enter their username and password to access their account.
🎯 Goal: Create a Django view to handle user login and a corresponding HTML template with a form for username and password.
📋 What You'll Learn
Create a Django view function named login_view that uses django.contrib.auth.authenticate and django.contrib.auth.login to log in users.
Create an HTML template named login.html with a form containing username and password input fields and a submit button.
The form should use the POST method and submit to the same URL.
If authentication fails, the view should re-render the template with an error message.
💡 Why This Matters
🌍 Real World
Login pages are essential for websites that require user accounts, such as social media, e-commerce, or online services.
💼 Career
Understanding how to implement user authentication is a key skill for web developers working with Django or similar frameworks.
Progress0 / 4 steps
1
Create the login view function
Create a Django view function called login_view that accepts a request parameter and imports authenticate and login from django.contrib.auth. For now, just return render(request, 'login.html').
Django
Hint
Start by importing authenticate and login from django.contrib.auth. Then define login_view that returns render(request, 'login.html').
2
Add POST check and get username and password
Inside the login_view function, add a check for request.method == 'POST'. If true, get username and password from request.POST using request.POST.get('username') and request.POST.get('password').
Django
Hint
Use request.method == 'POST' to check the form submission. Then get the username and password from request.POST.
3
Authenticate user and login or show error
Still inside the POST check, use authenticate(request, username=username, password=password) to verify credentials. If the user is valid, call login(request, user) and redirect to '/'. Otherwise, set an error variable to 'Invalid username or password' and pass it to the template context.
Django
Hint
Use authenticate to check credentials. If valid, call login and redirect. Otherwise, prepare an error message for the template.
4
Create the login.html template with form and error display
Create an HTML file named login.html with a form that uses method='post'. Include {% csrf_token %} inside the form. Add input fields with name='username' and name='password'. Add a submit button with text 'Login'. Below the form, display the error variable if it exists inside a <p> tag with style='color: red;'.
Django
Hint
Create a form with POST method, include {% csrf_token %}, username and password inputs, and a submit button. Show the error message in red if it exists.
Practice
(1/5)
1. What is the main purpose of Django's LoginView?
easy
A. To display a list of logged-in users
B. To create a new user registration form
C. To handle password reset requests
D. To provide a ready-made view for user login handling
Solution
Step 1: Understand Django's built-in views
Django provides LoginView as a built-in class-based view to handle user login functionality easily.
Step 2: Identify the purpose of LoginView
It manages displaying the login form, validating user credentials, and logging users in.
Final Answer:
To provide a ready-made view for user login handling -> Option D
Quick Check:
LoginView = ready-made login handler [OK]
Hint: LoginView is for login pages, not registration or reset [OK]
Common Mistakes:
Confusing LoginView with registration or password reset views
Thinking LoginView lists users
Assuming LoginView creates new users
2. Which of the following is the correct way to specify a custom template for Django's LoginView?
easy
A. template = 'login.html'
B. template_name = 'login.html'
C. templateFile = 'login.html'
D. templatePath = 'login.html'
Solution
Step 1: Recall Django's class-based view attribute for templates
Django's class-based views use the attribute template_name to specify the HTML template file.
Step 2: Match the correct attribute name
Among the options, only template_name is the correct attribute to set the template.
Final Answer:
template_name = 'login.html' -> Option B
Quick Check:
Use template_name to set template in CBVs [OK]
Hint: Remember: class-based views use template_name, not template [OK]
Common Mistakes:
Using 'template' instead of 'template_name'
Using camelCase like 'templateFile' or 'templatePath'
5. You want to customize the login form to add a 'Remember Me' checkbox that keeps users logged in longer. Which approach correctly integrates this feature using Django's LoginView?
hard
A. Override LoginView to add a custom form with a 'remember_me' field and adjust session expiry in form_valid
B. Add a checkbox in the template only; Django handles session duration automatically
C. Set template_name to a new template with the checkbox but do not change the view or form
D. Use Django's default LoginView without changes; 'Remember Me' is built-in
Solution
Step 1: Understand customizing LoginView
To add new fields like 'Remember Me', you must create a custom form and override LoginView to use it.
Step 2: Adjust session expiry based on 'remember_me'
Override form_valid method to set session expiry longer if 'remember_me' is checked.
Final Answer:
Override LoginView to add a custom form with a 'remember_me' field and adjust session expiry in form_valid -> Option A
Quick Check:
Custom form + override form_valid for 'Remember Me' [OK]
Hint: Customize form and override form_valid to handle 'Remember Me' [OK]
Common Mistakes:
Adding checkbox only in template without backend logic