0
0
Djangoframework~30 mins

CreateView for object creation in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Create a Django CreateView for Book Entry
📖 Scenario: You are building a simple web app to add new books to a library database. Users will fill a form to create a new book entry.
🎯 Goal: Create a Django CreateView to handle adding new Book objects with fields title and author.
📋 What You'll Learn
Define a Book model with title and author fields
Create a CreateView class named BookCreateView
Set the model attribute to Book
Set the fields attribute to ["title", "author"]
Set the success_url attribute to "/books/"
💡 Why This Matters
🌍 Real World
Creating forms to add new records is common in web apps like blogs, stores, or libraries.
💼 Career
Understanding Django's generic views helps build efficient, maintainable web applications quickly.
Progress0 / 4 steps
1
Define the Book model
Create a Django model called Book with two fields: title as CharField with max length 100, and author as CharField with max length 100.
Django
Need a hint?

Use models.CharField(max_length=100) for both fields inside the Book class.

2
Import CreateView and set up BookCreateView
Import CreateView from django.views.generic. Then create a class called BookCreateView that inherits from CreateView.
Django
Need a hint?

Use from django.views.generic import CreateView and define class BookCreateView(CreateView):.

3
Configure model and fields in BookCreateView
Inside BookCreateView, set the model attribute to Book and the fields attribute to ["title", "author"].
Django
Need a hint?

Set model = Book and fields = ["title", "author"] inside the BookCreateView class.

4
Add success_url to BookCreateView
Add a success_url attribute to BookCreateView and set it to "/books/" so the user is redirected after creating a book.
Django
Need a hint?

Set success_url = "/books/" inside the BookCreateView class.