reading-notes2

https://m7madmomani2.github.io/reading-notes2

View the Project on GitHub M7madMomani2/reading-notes2

Django CRUD and Forms

Django form handling process


ModelForms

A basic ModelForm containing the same field as our original RenewBookForm is shown below. All you need to do to create the form is add class Meta with the associated model (BookInstance) and a list of the model fields to include in the form.

from django.forms import ModelForm
from catalog.models import BookInstance

class RenewBookModelForm(ModelForm):
    class Meta:
        model = BookInstance
        fields = ['due_back']

Templates

The “create” and “update” views use the same template by default, which will be named after your model: model_name_form.html (you can change the suffix to something other than _form using the template_name_suffix field in your view, for example template_name_suffix = ‘_other_suffix’)