0
0
Djangoframework~20 mins

CreateView for object creation in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CreateView Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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')
AThe user sees the detail page of the newly created Book object.
BThe form is cleared and the same form page is shown again with a success message.
CThe user is redirected to the URL named 'book-list'.
DThe form page reloads with validation errors.
Attempts:
2 left
💡 Hint
Check the purpose of the 'success_url' attribute in CreateView.
📝 Syntax
intermediate
2: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)
A
def form_valid(self, form):
    print('Form is valid')
    return super().form_valid(form)
B
def form_valid(self, form):
    print('Form is valid')
    form.save()
    return super().form_valid(form)
C
def form_valid(self, form):
    print('Form is valid')
    return self.form_valid(form)
D
def form_valid(self, form):
    print('Form is valid')
    return form.save()
Attempts:
2 left
💡 Hint
Remember to call the parent method to keep the default behavior.
🔧 Debug
advanced
2: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()
ABecause form_valid returns the saved object instead of an HTTP response.
BBecause success_url is a string instead of a reverse_lazy object.
CBecause fields attribute is missing a required field.
DBecause the model Product is not imported correctly.
Attempts:
2 left
💡 Hint
Check what form_valid is expected to return.
state_output
advanced
2: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
A'self.object' is None until the view finishes processing.
B'self.object' holds the newly created and saved model instance.
C'self.object' holds the form instance, not the model instance.
D'self.object' holds the HTTP response returned by form_valid.
Attempts:
2 left
💡 Hint
Check what CreateView sets after saving the form.
🧠 Conceptual
expert
2:00remaining
Which statement about Django's CreateView is TRUE?
Select the correct statement about how Django's CreateView works.
ACreateView renders the form using the model's __str__ method by default.
BCreateView requires manually calling form.save() in the post method to save the object.
CCreateView does not support specifying fields; you must create a custom form class.
DCreateView automatically saves the form and redirects to success_url if the form is valid.
Attempts:
2 left
💡 Hint
Think about the default behavior of CreateView on valid form submission.