Python Basics (Django II)

Source: Internet
Author: User

This article is connected to the Python Foundation (Django)


Vi. Form

Description: The From in Django has two functions, one is to display itself as HTML, and the other is to verify the data.

Personally think that the display of HTML is not very convenient, because the display style is not very good adjustment, it is recommended to use only the function of the validation data form, so here for style settings do not introduce, highlighting the function of verifying data.

The use of the form is roughly divided into three steps:

Define a class first: Each field in the class is a property of the form

The object is then created based on the class and the validation method is defined: The HTML page reference that returns the created object to the front end generates the appropriate HTML code. (You can also validate and obtain data submitted by the front-end user by invoking the method of the class.) )

Finally referenced in HTML: If the tags and styles in the HTML are customized, then the value of the label's Name property must match the field name in the form, otherwise the background will not be able to obtain and perform data validation.


Example:

1. Define a class:

File name: account.py

The From Django Import forms #导入forms类class Userinfo (forms. Form): #新创建的类必须继承forms. Form email = forms. Emailfield (Required=false) #默认是不允许为空, if the Required=false parameter is added, the Allow is null username = forms. Charfield () #forms有多种字段类型, each of which represents a different check rule password = forms. Charfield ()

2. Create objects based on class and define the calibration method

File name: views.py

from django.shortcuts import render,httpresponseredirectfrom app01 import  account as accountform     #导入事先定义的类文件def  login (req):      #定义校验方法     obj = accountform.userinfo (req. Post)      #获取用户提交的数据     if req.method ==  ' Post ':                  #如果用户的请求类型是POST         error =  '          if obj.is_valid ():             # Verifying that user-submitted data is legitimate             all_data =  Obj.clean ()      #获取用户提交的数据内容 (dictionary form)              if all_data[' username '] ==  ' test '  and all_data[' password '] ==  ' 123 ':                 return httpresponseredirect ('/index/')     # Verify pass then jump to url:/index/        else:             error = obj.errors     #若数据不合法则获取错误信息              return render (req, ' account/login.html ', {' ERROR ': Error})      #在页面上显示数据校验失败后的提示信息.             # return render (req, ' account /login.html ', {' obj ': obj})   #这种方式表示把创建的对象返回给前端的HTML页面引用 to generate the appropriate HTML code.     return render (req, ' account/login.html ')      #这种方式表示自定义页面标签及样式, Instead of using objects returned in the background to generate HTML code.

3, referencing in HTML

<! Doctype html>


Vii. Model (to Be continued)

This article from "A rookie on the Sky" blog, please be sure to keep this source http://rmeos.blog.51cto.com/761575/1754546

Python Basics (Django II)

Related Article

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.