Book Image

Django Essentials

By : Samuel Dauzon
Book Image

Django Essentials

By: Samuel Dauzon

Overview of this book

<p>Django is a powerful Python web framework designed for rapid web application development. With the advent of frameworks such as Django, web developers have been forced to adopt MVC architectures and are encouraged to develop quality code. This quality allows several developers to work together easily and reduces the number of bugs due to human errors.</p> <p>Beginning with the basics of the Web and Django, the book explains the MVC pattern. It then moves on to explain the step-by-step installation of Python, PIP, and Django on Windows, Linux, and Mac OS. Furthermore, you will learn how to create templates, models, forms, and so on. After reading the book, you will be able to quickly create dynamic web applications with AJAX and an admin part.</p> <p>This book features a step-by-step approach that shows you how to program, create, and improve the quality of web applications using Django, with the help of Python.</p>
Table of Contents (20 chapters)
Django Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 that validates the field value

  • widget: This defines a specific widget for the field

The widget form

Widgets allow you to define HTML code that renders form fields. We'll explain what widgets can generate as HTML code, as follows:

  • TextInput: This corresponds to <input type="text" />

  • Textarea: This corresponds to <textarea></textarea>

  • PasswordInput: This corresponds to <input type="password" />

  • RadioSelect: This corresponds to <input type="radio" />

  • Select: This corresponds to <select><option></option></select>

  • CheckboxInput: This corresponds to <input type="checkbox" />

  • FileInput: This corresponds to <input type="file" />

  • HiddenInput: This corresponds to <input type="hidden" />

Error messages (forms and models)

The following is a partial list of the error messages that can be set when form fields are entered incorrectly:

  • required: This message is displayed when the user does not fill data in the field

  • min_length: This message is displayed when the user has not supplied enough data

  • max_length: This message is displayed when the user has exceeded the size limit of a field

  • min_value: This message is displayed when the value entered by the user is too low

  • max_value: This message is displayed when the value entered by the user is too high