Skip to the content.

What we will learn

The source of this summary The first link


Working with forms

Forms are basically used for taking input from the user in some manner and using that information for logical operations on databases. For example, Registering a user by taking input as his name, email, password, etc. Django maps the fields defined in Django forms into HTML input fields

Syntax

  from django import forms
    
  class FormName(models.Model):
           # each field would be mapped as an input field in HTML
          field_name = models.Field(**options)

Form

The Form class is the heart of Django’s form handling system. It specifies the fields in the form, their layout, display widgets, labels, initial values, valid values, and (once validated) the error messages associated with invalid fields. The class also provides methods for rendering itself in templates using predefined formats (tables, lists, etc.) or for getting the value of any element (enabling fine-grained manual rendering).

Declaring a Form

Creating a form in Django is completely similar to creating a model, one needs to specify what fields would exist in the form and of what type.

Form fields

URL configuration

urlpatterns += [
path('book/<uuid:pk>/renew/', views.renew_book_librarian, name='renew-book-librarian'),
  ]