Challenge - 5 Problems
UserCreationForm Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens after a successful UserCreationForm submission?
Consider a Django view using
UserCreationForm to register a new user. What is the typical behavior after the form is successfully saved?Django
from django.contrib.auth.forms import UserCreationForm from django.shortcuts import render, redirect def register(request): if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid(): form.save() return redirect('login') else: form = UserCreationForm() return render(request, 'register.html', {'form': form})
Attempts:
2 left
💡 Hint
Think about what
redirect('login') does after saving the form.✗ Incorrect
When the form is valid and saved, the user is created in the database. Then the view redirects the browser to the login page, so the user can sign in.
📝 Syntax
intermediate1:30remaining
Which code correctly initializes UserCreationForm with POST data?
You want to create a registration form in Django using
UserCreationForm. Which option correctly initializes the form with POST data inside a view?Attempts:
2 left
💡 Hint
Check the official Django docs for the correct parameter name.
✗ Incorrect
The UserCreationForm expects the POST data as a keyword argument data=. Passing it as a positional argument is not recommended.
❓ state_output
advanced2:00remaining
What is the value of
form.errors after submitting invalid data?Given this code snippet in a Django view handling registration, what will
form.errors contain if the passwords do not match?Django
form = UserCreationForm(request.POST) if not form.is_valid(): errors = form.errors
Attempts:
2 left
💡 Hint
UserCreationForm checks if the two password fields match and adds errors accordingly.
✗ Incorrect
If the passwords do not match, form.errors will include an error message under the password2 field explaining the mismatch.
🔧 Debug
advanced2:30remaining
Why does this registration view raise a TypeError?
Examine the code below. Why does it raise a
TypeError when submitting the form?Django
def register(request): form = UserCreationForm() if request.method == 'POST': form = UserCreationForm(request.POST, request.FILES) if form.is_valid(): form.save() return redirect('login') return render(request, 'register.html', {'form': form})
Attempts:
2 left
💡 Hint
Check the parameters accepted by UserCreationForm constructor.
✗ Incorrect
UserCreationForm only accepts one positional argument for data. Passing request.FILES as a second argument causes a TypeError.
🧠 Conceptual
expert3:00remaining
Which statement about UserCreationForm's password handling is true?
Select the correct statement about how Django's
UserCreationForm manages passwords during user registration.Attempts:
2 left
💡 Hint
Think about the purpose of entering the password twice in registration forms.
✗ Incorrect
UserCreationForm asks for the password twice to confirm the user typed it correctly. It hashes the password before saving and does not log in the user automatically.