Hint: Use CreateView with fields='__all__' for all editable fields [OK]
Common Mistakes:
Using UpdateView instead of CreateView
Using 'exclude' instead of 'fields'
Forgetting to set success_url
3. Given this CreateView code:
class AuthorCreateView(CreateView):
model = Author
fields = ['name', 'email']
success_url = '/authors/'
What happens after a user submits the form with valid data?
medium
A. A new Author object is saved and user is redirected to '/authors/'
B. The form is cleared but user stays on the same page
C. An error occurs because success_url is missing
D. The form data is saved but user sees the same form again
Solution
Step 1: Understand CreateView behavior on valid form submission
CreateView saves the new object and redirects to success_url if form is valid.
Step 2: Check the provided success_url
success_url is set to '/authors/', so after saving, user is redirected there.
Final Answer:
A new Author object is saved and user is redirected to '/authors/' -> Option A
Quick Check:
Valid form submission = save + redirect [OK]
Hint: Valid form submits save object and redirect to success_url [OK]
Common Mistakes:
Thinking form clears but stays on page
Assuming success_url is optional
Believing form data saves but no redirect happens
4. Identify the error in this CreateView code:
class PublisherCreateView(CreateView):
model = Publisher
fields = ['name', 'address']
template_name = 'publisher_form.html'
medium
A. Missing success_url attribute causes error after form submission
B. fields list should be a tuple, not a list
C. template_name should be 'publisher_create.html' exactly
D. model attribute must be a string, not a class
Solution
Step 1: Check required attributes for CreateView
CreateView requires success_url to know where to redirect after saving.
Step 2: Validate other attributes
fields can be list or tuple, template_name can be any valid template name, model must be a class, so no errors there.
Final Answer:
Missing success_url attribute causes error after form submission -> Option A
Quick Check:
Missing success_url = error on submit [OK]
Hint: Always set success_url in CreateView to avoid redirect errors [OK]
Common Mistakes:
Omitting success_url
Thinking fields must be tuple only
Assuming template_name must follow fixed naming
5. You want to create a CreateView for a Product model but only allow users to enter name and price. After saving, redirect to the product's detail page at /product/<id>/. Which is the best way to implement success_url?
hard
A. Set success_url = reverse_lazy('product-detail', args=[self.object.id]) inside the view
B. Set success_url = '/product/' + str(self.object.id) + '/' as a class attribute
C. Set success_url = '/product/<id>/' literally as a string
D. Override get_success_url method to return reverse('product-detail', args=[self.object.id])
Solution
Step 1: Understand dynamic success_url needs
Since the URL depends on the saved object's id, success_url must be dynamic, not a fixed string.
Step 2: Choose correct method to generate dynamic URL
Overriding get_success_url allows access to self.object.id and returns the correct URL using reverse().
Step 3: Evaluate other options
Set success_url = reverse_lazy('product-detail', args=[self.object.id]) inside the view uses reverse_lazy with self.object.id at class level (invalid), Set success_url = '/product/' + str(self.object.id) + '/' as a class attribute tries string concat at class level (no self.object), Set success_url = '/product/<id>/' literally as a string uses literal string without id.
Final Answer:
Override get_success_url method to return reverse('product-detail', args=[self.object.id]) -> Option D
Quick Check:
Dynamic URL needs get_success_url override [OK]
Hint: Use get_success_url() to build URLs needing object data [OK]