The Django Forms module uses

Source: Internet
Author: User

Generating an HTML input tag from a Django Froms module

First create a forms.py file under the current application

Vim forms.py

From Django Import forms

def forms. Adminform (forms. Form):

Username = forms. Charfield () #生成明文的输入框

Password = forms. Charfield (Widget=forms.widgets.passwordinput)

#生成一个密文显示的输入框

Password = forms. Charfield (Widget=forms.widgets.passwordinput (attrs={' class ': ' Auto '})

(Attrs={' class ': ' Auto '}) #定义标签属性

Email1 = forms. Charfield (Widget=forms.widgets.emailinput)

EMAIL2 = forms. Emailfield ()

#widget类型是通过正则表达式来判定的





Views.py write a method for generating the input tag of the forms module

def index2 (Request):

if Request.method = = ' POST ':

#判断在html表单里面提交的方式是什么 (typically 2 way post and get)

form = forms. Adminform (Request. POST)

A class in the #AdminForm是在froms. PY, invoked through the forms method and assigned to a form

If Forms.is_valid ():

#判断是否合法, the error message will be prompted on the front page of the wrong input

data = Form.cleaned_data #请求通过接受该数据

m = Adminform (username= ' Tom ', password= ' tom123 ')

#AdminForm里面提交数据, and save

M.save ()

else: #如果输入规则不匹配在前台显示错误信息

Print Form.erros.as_data ()

Print Form.erros.as_json ()

Print Form.erros.as_text ()

Common error message Formats

Else

form = forms. Adminform ()

#判断如果不是post方法提交的

return render_to_response (' index2.html ', {' Model ': form})

#把结果返回给前端的index2. Html,model is a variable inside the front index2.html that accepts data returned in the background, and the form is the real data





3.index2.html Content


<meta http-equiv= "Content-type" content= "text/html"/>

<title></title>

<body>

<form action= '/index2/' method= ' POST ' >

<p> User name:{{model.username}}</p>

<p> Password:{{model.password}}</p>

<p> Email:{{model.email}}</p>

<input type= ' Submit ' value= ' submission '/>

</form>

</body>



After the optimization, more concise save a lot of HTML code


<meta http-equiv= "Content-type" content= "text/html"/>

<title></title>

<body>

<form action= '/index2/' method= ' POST ' >

{{model.as_table}} has been shown in table Form

{{Model.as_ul}} has been shown in UL form

#具体查看form父类里面的详细方法---source code

#好处省代码

#坏处样式不可控

<input type= ' Submit ' value= ' submission '/>

</form>

</body>


The Django Forms module uses

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.