The basic implementation mechanism and basic concept of Django form

Source: Internet
Author: User
Tags html form input

Based on Django1.9 Doc-form

This article simply makes clear the key concepts and key processes in the Django form system, without paying attention to the details

The purpose is to have an intuitive understanding of the form implementation mechanism of Djano, which can be grasped from the whole.

If you want to learn more about code implementation and various technical details, you can read the official Django documentation.

1 Why should I use Django Form? What happens when you use Django Form?

Unless you ' re planning to build websites and applications that does nothing but publish content, and don ' t accept input from Your visitors, you ' re going to need to understand and use forms.

2 Basic concepts: What is HTML Form?
HTML form reference: W3school HTML form

A form must specify and things:

    • Where:the URL to which the data corresponding to the user's input should be returned
    • How:the HTTP method The data should is returned by

3 HTTP Get/post
Can be used to submit form data,
Whether you want to keep the data in the server or not, consider security considerations.

4 The basic process for Django processing forms:
Django handles three distinct parts of the work involved in forms:

    • Preparing and restructuring data to make it ready for rendering
    • Creating HTML forms for the data
    • Receiving and processing submitted forms and data from the client

5 Form Class
At the heart of this system of are Django ' s Form class
A form class describes a form and determines how it works and appears.

The form-class-based process flow in Django:



6 What is Modelform? Why use it?

If you ' re building a Database-driven app, chances is you'll have the forms that maps closely to Django models. For instance, you might has a blogcomment model, and you want to create a form that lets people submit comments (your form is going to being used to directly add or edit a Django model,). In this case, it would is redundant to define the field types in your form, because you ' ve already defined the fields in Y Our model.
For this reason, the Django provides a helper class that lets you create a Form class from a Django model.

A modelform can save you a great deal of time, effort, and code, because it would build a form, along with the appropriate Fields and their attributes, from a Model class.

7 What are form fields?
A Form class ' s fields map to HTML form <input> elements.

    • A form ' s fields is themselves classes; They manage form data and perform validation when a form is submitted.
    • A Datefield and a filefield handle very different kinds of data and has to does different things with it.


8 What is Widgets?
A Widget is the Django ' s representation of an HTML INPUT element.
The widget handles the rendering of the HTML, and the extraction of data from a Get/post dictionary that corresponds to th E widget.

Each form field have a corresponding widget class, which in turn corresponds to an HTML form widget such as <input type= "Text";.

In most cases, the field would have a sensible default widget. For example, by default, a Charfield would have a TextInput widget, which produces an <input type= "text" > in the HTML. If you needed <textarea> instead, you ' d specify the appropriate widget when defining your form field, as we had do NE for the message field.


9 Form fields and Widgets:
Form Fields------------deal with the logic of the input validation and is used directly in templates.
Widgets----------------deal with rendering of HTML form input elements on the Web page and extraction of Raw submitted DA Ta.
However, widgets do need to is assigned to form fields.

The basic implementation mechanism and basic concept of Django form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.