Django Form的基本实现机制和基本概念

基于Django1.9 Doc-Form

本文只是理清Django Form系统中的关键概念和关键流程,没有关注其中的细节实现,

目的是对Djano的Form实现机制能有一个直观的了解,能够从从整体上把握它。

如果想深入了解代码实现及各种技术细节,可以阅读Django官方文档。

1 为什么要使用Django Form? 什么情况下要使用Django Form?

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

2 基本概念:What is HTML Form?
HTML Form参考:w3school HTML Form

A form must specify two things:

  • where: the URL to which the data corresponding to the user’s input should be returned
  • how: the HTTP method the data should be returned by

3 HTTP GET/POST
都可以用来提交表单数据,
是否要在服务器中保存数据,是否要考虑安全因素。

4 Django 处理表单的基本流程:
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 components is Django’s Form class 
a Form class describes a form and determines how it works and appears.

Django中基于Form Class的表单处理流程:

6 What is ModelForm? Why use it?

If you’re building a database-driven app, chances are you’ll have forms that map closely to Django models. For instance, you might have a BlogComment model, and you want to create a form that lets people submit comments(your form is going to be used to directly add or edit a Django model, ). In this case, it would be redundant to define the field types in your form, because you’ve already defined the fields in your model.
For this reason, 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 will build a form, along with the appropriate fields and their attributes, from a Model class.

7 What is form fields?
A form class’s fields map to HTML form <input> elements.

  • A form’s fields are 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 have to do different things with it.

8 What is Widgets?
A widget is 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 the widget.

Each form field has a corresponding Widget class, which in turn corresponds to an HTML form widget such as <input type="text">.

In most cases, the field will have a sensible default widget. For example, by default, a CharField will have a TextInput widget, that 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 have done for the message field.

9 Form Fields and Widgets:
Form fields ------------deal with the logic of input validation and are used directly in templates.
Widgets---------------- deal with rendering of HTML form input elements on the web page and extraction of raw submitted data.
However, widgets do need to be assigned to form fields.

时间: 2024-10-06 17:20:24

Django Form的基本实现机制和基本概念的相关文章

Django——form组件is_valid校验机制

#先来归纳一下整个流程#(1)首先is_valid()起手,看seld.errors中是否值,只要有值就是flase#(2)接着分析errors.里面判断_errors是都为空,如果为空返回self.full_clean(),否则返回self._errors#(3)现在就要看full_clean(),是何方神圣了,里面设置_errors和cleaned_data这两个字典,一个存错误字段,一个存储正确字段.#(4)在full_clean最后有一句self._clean_fields(),表示校验

Django Form表单

Django  Form 表单 在实际的生产环境中比如登录和验证的时候,我们一般都使用Jquery+ajax来判断用户的输入是否为空,假如JS被禁用的话,咱们这个认证屏障是不是就消失了呢?(虽然一般不会禁用掉但是还是存在风险) 所以我们一般做两种认证一种是前端做一遍认证,在后端做一遍认证. 首先咱们看一下下面的案例: from django.shortcuts import render # Create your views here. def user_list(request): host

Django Form表单基础

平时我们写表单要自己写样式,比如我们要写一个注册样式,有如下填写项: 实现代码如下: views.py文件 #!/usr/bin/env python #-*-conding:utf-8:-*- from django.shortcuts import render from django import forms # Create your views here. def user_list(request):     host = request.POST.get('host')     p

django captcha和邮箱验证机制

验证码插件--django captcha和邮箱验证机制 对于web开发在用户注册登录的环节的验证码是在开发中必不可少的一个环节,这里介绍一下我在开发中经常使用到的一个带三方的验证码插件-- captcha 一.安装 在GitHub上可以直接下载: 也可以使用pip进行安装: 二.导入 1.settings.py文件 这里需要在INSTALLED_APPS中将其注册进去: INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.aut

django form

ModelForm的继承者有两种保存数据方式: 一:创建新的数据: f = ArticleForm(request.POST)new_article = f.save() 二:更新数据库:a = Article.objects.get(pk=1)f = ArticleForm(request.POST, instance=a)f.save() 保存时候的commit选项: f = AuthorForm(request.POST)new_author = f.save(commit=False)n

Django form模块使用心得

最近用Django 写了一个网站,现在来分享一下对Django form 的一些心得. 一,创建一个表单 创建一个Form表单有两种方式: 第一种方式是继承于forms.Form,的一个子类,通过在form中选择你需要的类型来规定表单之 中字段的类型 Java代码   class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField(required=False)

Django ==&gt; Form 组件

Django ==> Form 组件 目录: 1.基本使用 2.form中字段和插件 3.自定义验证规则 4.动态加载数据到form中 Action: 1.基本使用 django 中的Form组件有一下功能: 1.生成html标签 2.验证用户数据(显示错误信息) 3.html form 提交保留上次提交数据 4.初始化页面显示内容 要使用 form 类,首先需要创建这个类,方法如下: from django.forms import Form from django.forms import

【python】-- Django Form

Django  Form Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容(自定义样式) 一.Form 简单示例: 1.view中创建Form类并进行函数处理 class FM(forms.Form): user = fields.CharField() pwd = fields.CharField() email = fields.EmailField() def fm(request):

6月28日 Django form表单

Form介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否输入,输入的长度和格式等正不正确.如果用户输入的内容有错误就需要在页面上相应的位置显示对应的错误信息.. Django form组件就实现了上面所述的功能. 总结一下,其实form组件的主要功能如下: 生成页面可用的HTML标签 对用户提交的数据进行校验 保留上次输入内容 普通的登录 views.p