Hint: Use success_url with reverse_lazy for redirects [OK]
Common Mistakes:
Using reverse instead of reverse_lazy in class attributes
Using wrong attribute names like redirect_url
Assigning plain strings without URL reversing
3. Given this DeleteView code snippet, what happens when the user confirms deletion?
class BookDeleteView(DeleteView):
model = Book
template_name = 'books/book_confirm_delete.html'
success_url = reverse_lazy('book-list')
medium
A. The book is deleted and user is redirected to the book list page
B. The book is updated and user stays on the same page
C. The book is deleted but user stays on the confirmation page
D. Nothing happens because success_url is incorrect
Solution
Step 1: Confirm DeleteView behavior on confirmation
When the user confirms, the object specified by model is deleted.
Step 2: Check success_url usage
After deletion, the user is redirected to the URL given by success_url, here 'book-list'.
Final Answer:
The book is deleted and user is redirected to the book list page -> Option A
Quick Check:
Delete + redirect = The book is deleted and user is redirected to the book list page [OK]
Hint: Confirm deletes object and redirects to success_url [OK]
Common Mistakes:
Thinking the object is updated instead of deleted
Assuming user stays on confirmation page after delete
Believing success_url must be a string, not reverse_lazy
4. Identify the error in this DeleteView subclass:
class ArticleDeleteView(DeleteView):
model = Article
template_name = 'articles/delete.html'
success_url = reverse('article-list')
medium
A. template_name should be 'article_confirm_delete.html'
B. Using reverse() instead of reverse_lazy() for success_url
C. Missing the get_object() method override
D. model attribute should be a string, not a class
Solution
Step 1: Check success_url assignment in class attribute
Class attributes are evaluated at import time, so reverse() causes errors here.
Step 2: Use reverse_lazy() for lazy URL resolution
reverse_lazy() delays evaluation until runtime, fixing the error.
Final Answer:
Using reverse() instead of reverse_lazy() for success_url -> Option B
Quick Check:
reverse_lazy needed for class attributes [OK]
Hint: Use reverse_lazy in class attributes, not reverse [OK]
Common Mistakes:
Overriding get_object() unnecessarily
Assuming template_name must follow a strict name
Using model as string instead of class (both work but class preferred)
5. You want to customize the confirmation page of a DeleteView to show extra context data like the current user's name. Which method should you override to add this data?
hard
A. get_context_data()
B. get_object()
C. form_valid()
D. dispatch()
Solution
Step 1: Understand where to add extra template data
Extra data for templates is added by overriding get_context_data().
Step 2: Confirm other methods' purposes
get_object() fetches the object, form_valid() handles form submission, and dispatch() manages request flow.
Final Answer:
get_context_data() -> Option A
Quick Check:
Extra template data = get_context_data() [OK]
Hint: Add extra template info by overriding get_context_data() [OK]