Challenge - 5 Problems
CreateView Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Django CreateView render after a successful form submission?
Given this Django CreateView, what will the user see after successfully creating a new Book object?
Django
from django.views.generic.edit import CreateView from django.urls import reverse_lazy from .models import Book class BookCreateView(CreateView): model = Book fields = ['title', 'author'] template_name = 'books/book_form.html' success_url = reverse_lazy('book-list')
Attempts:
2 left
💡 Hint
Check the purpose of the 'success_url' attribute in CreateView.
✗ Incorrect
The 'success_url' attribute tells Django where to redirect after a successful form submission. Here, it redirects to the URL named 'book-list'.
📝 Syntax
intermediate2:00remaining
Which option correctly overrides the form_valid method in a Django CreateView?
You want to add a print statement when the form is valid before saving. Which code snippet correctly overrides form_valid?
Django
from django.views.generic.edit import CreateView from .models import Article class ArticleCreateView(CreateView): model = Article fields = ['title', 'content'] def form_valid(self, form): # Your code here return super().form_valid(form)
Attempts:
2 left
💡 Hint
Remember to call the parent method to keep the default behavior.
✗ Incorrect
Option A correctly prints a message and then calls the parent form_valid method, which saves the form and redirects. Option A calls form.save() twice, causing duplicate saves. Option A returns the saved object instead of an HTTP response. Option A causes infinite recursion.
🔧 Debug
advanced2:00remaining
Why does this CreateView raise a TypeError when submitting the form?
Examine the code and identify why submitting the form causes a TypeError.
Django
from django.views.generic.edit import CreateView from .models import Product class ProductCreateView(CreateView): model = Product fields = ['name', 'price'] success_url = '/products/' def form_valid(self, form): return form.save()
Attempts:
2 left
💡 Hint
Check what form_valid is expected to return.
✗ Incorrect
The form_valid method must return an HTTP response, usually by calling super().form_valid(form). Returning form.save() returns the saved object, causing a TypeError.
❓ state_output
advanced2:00remaining
What is the value of 'self.object' after form_valid is called in a CreateView?
In a Django CreateView, after form_valid is called, what does 'self.object' hold?
Django
class MyCreateView(CreateView): model = MyModel fields = ['field1', 'field2'] def form_valid(self, form): response = super().form_valid(form) # What is self.object here? return response
Attempts:
2 left
💡 Hint
Check what CreateView sets after saving the form.
✗ Incorrect
CreateView sets 'self.object' to the saved model instance after form_valid is called, so you can access the created object in your method.
🧠 Conceptual
expert2:00remaining
Which statement about Django's CreateView is TRUE?
Select the correct statement about how Django's CreateView works.
Attempts:
2 left
💡 Hint
Think about the default behavior of CreateView on valid form submission.
✗ Incorrect
CreateView handles form saving and redirection automatically when the form is valid. You don't need to override post or call form.save() manually. You can specify fields or a form class. The form is rendered using a template, not the model's __str__ method.