The form fields

It is possible to use all types of field models in the forms. Indeed, some types of model fields have been created for a particular use in forms. For example, the TextField model field has nothing different from CharField except the fact that by default, in the form, the TextField field displays a <textarea> tag and a <input type="text"> name. So, you can write a form field as follows:

field1 = forms.TextField()

Common options for the form fields

The following options are common to all the form fields:

  • error_messages: This specifies a series of error messages
  • help_text: This sets a help message
  • required: This defines a field that must be filled
  • initial: This sets the default value for the field
  • validators: This defines a particular validator ...

Get Django: Web Development with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.