From the course: Django Essential Training
Unlock this course with a free trial
Join today to access over 24,700 courses taught by industry experts.
Django forms: Powerful validation with minimal work
From the course: Django Essential Training
Django forms: Powerful validation with minimal work
- [Instructor] Adding a new endpoint was nice and easy, but now it's time to consider more complex scenarios. Model forms are the best way of doing this in Django. Let's check it out. First, we're going to create a file called forms and inside our Notes app. Okay, so in here, let's add from django import forms and from .models import Notes. With this, we can create a new class called NotesForm that's going to inherit from forms.ModelForm, and inside this class, we're going to create a new class, Meta. Okay. That's going to receive model, which is Notes, and fields, just like we added on the class-based view for Create View. So title, and text. Okay. With this we can come back to the views.py file, and in here, instead of passing the fields, we're going to pass a form_class. That's going to be our NotesForm. We also need to import it, so from .forms import Notesform. Okay. So far what we did will result in exactly the same behavior as we have so far, but the form will give us power to…