forms.py
表单模板
from django import forms from mysite import models
建立窗体类Form
#!/usr/bin/env python
# -*-coding:utf-8-*-
class ContactForm(forms.Form):
CITY = [
['TP','Taipei'],
['TY','Taoyuang'],
['TC','Taichung'],
['TN','Tainan'],
['KS','Kaohsiung'],
['NA','Others'],
]
user_name = forms.CharField(label = '您的姓名',max_length = 50,initial = '李大仁')
user_city = forms.ChoiceField(label = '居住城市',choices = CITY)
user_school = forms.BooleanField(label = '是否在学',required = False)
user_email = forms.EmailField(label = '电子邮件')
user_message = forms.CharField(label = '您的意见',widget = forms.Textarea)
常用的字段格式设置方法
使用ModelForm来产生窗体
class PostForm(forms.ModelForm):
## captcha = CaptchaField()
class Meta:
mood = models.Mood ##指定窗口要引入哪个models
model = models.Post
fields = ['mood','nickname','message','del_pass'] ##指定使用models中的哪个字段
##以下是把默认的英文字段名改为中文字段名
def __init__(self, *args, **kwargs):
super(PostForm,self).__init__(*args,**kwargs)
self.fields['mood'].label = '现在的心情'
self.fields['nickname'].label = '您的昵称'
self.fields['message'].label = '心情留言'
##self.fields['enabled'].label = "是否直接显示在主页"
self.fields['del_pass'].label = '设置密码'
##self.fields['captcha'].label = '请输入验证码'
网友评论