Django
Form 主要有两种处理方式:
1.直接继承 forms.Form
2.直接利用已有的model中的class,从ModelForm中继承.
这两种方式,都涉及到如下几个问题:
1. django form 显示中文字段名
2. django form field 的样式控制
3. django form 的数据校验
form.html 模板如下:
程序代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" " http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Add contact</title>
</head>
<body>
<div><h2> 直接forms.Form 方式 </h2></div>
{% if form.errors %}
<p style="color: red;">
Please correct the error{{ form.errors|pluralize }} below.
</p>
{% endif %}
<form method="POST" action="/save_contact/">
{% csrf_token %}
<table>
{{form.as_table}}
</table>
<input type="submit" value="submit"/>
</form>
</body>
</html>
1.直接继承 forms.Form
2.直接利用已有的model中的class,从ModelForm中继承.
这两种方式,都涉及到如下几个问题:
1. django form 显示中文字段名
2. django form field 的样式控制
3. django form 的数据校验
4.django form 数据提交后的处理.
class TestForm(forms.Form):
#==label 用来控制 form 字段的 中文显示, widget 可以控制其他属性,比如样式等。
yourname = forms.CharField(label='名称',max_length=50, widget=forms.TextInput( attrs={'style':'border:1px solid #ccc;'}))
website = forms.URLField(required=False, widget=forms.TextInput( attrs={'class':'myclass'}))
email = forms.EmailField(label='邮箱')
content = forms.CharField(label='内容', widget=forms.Textarea( attrs={'cols':'80','rows':'5'}))
def add_comment(request):
context={}
context.update(csrf(request))
form = TestForm()
context['form']=form
return render_to_response('form.html',context)
def save_comment(request):
form = TestForm(request.POST)
#===校验form 中的数据,并处理.
if form.is_valid() :
print 'successs'
yourname = form.cleaned_data['yourname']
website = form.cleaned_data['website']
email = form.cleaned_data['email']
content = form.cleaned_data['content']
#===想干嘛就干嘛,可以插入数据库等
print 'get information:',yourname,email,website,content
return render_to_response('form.html',locals())
else:
print 'error'
return render_to_response('form.html',locals())
#==label 用来控制 form 字段的 中文显示, widget 可以控制其他属性,比如样式等。
yourname = forms.CharField(label='名称',max_length=50, widget=forms.TextInput( attrs={'style':'border:1px solid #ccc;'}))
website = forms.URLField(required=False, widget=forms.TextInput( attrs={'class':'myclass'}))
email = forms.EmailField(label='邮箱')
content = forms.CharField(label='内容', widget=forms.Textarea( attrs={'cols':'80','rows':'5'}))
def add_comment(request):
context={}
context.update(csrf(request))
form = TestForm()
context['form']=form
return render_to_response('form.html',context)
def save_comment(request):
form = TestForm(request.POST)
#===校验form 中的数据,并处理.
if form.is_valid() :
print 'successs'
yourname = form.cleaned_data['yourname']
website = form.cleaned_data['website']
email = form.cleaned_data['email']
content = form.cleaned_data['content']
#===想干嘛就干嘛,可以插入数据库等
print 'get information:',yourname,email,website,content
return render_to_response('form.html',locals())
else:
print 'error'
return render_to_response('form.html',locals())
form.html 模板如下:
![程序代码](http://www.yihaomen.com/article/python/images/code.gif)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" " http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Add contact</title>
</head>
<body>
<div><h2> 直接forms.Form 方式 </h2></div>
{% if form.errors %}
<p style="color: red;">
Please correct the error{{ form.errors|pluralize }} below.
</p>
{% endif %}
<form method="POST" action="/save_contact/">
{% csrf_token %}
<table>
{{form.as_table}}
</table>
<input type="submit" value="submit"/>
</form>
</body>
</html>