普通标签

  1. from django import forms
  2. class form_view(forms.Form):
  3. name = forms.CharField(
  4. required=True, # 输入不能为空
  5. label="姓名:", # 页面上的input框名字
  6. initial="张三", # 框里的默认值
  7. help_text="输入姓名", # 输入框后的帮助文本
  8. min_length=6, # 最小长度为6
  9. error_messages={"min_length":"长度最少6位","required":"输入值不能为空!"}, # 错误提示信息
  10. widget=forms.widgets.TextInput() # 输入框为普通文本
  11. )

秘文标签

  1. password = forms.CharField(
  2. label="密码:",
  3. min_length=8,
  4. max_length=10,
  5. error_messages={"min_length": "长度最少8位","max_length":"长度最多10位" ,"required": "输入值不能为空!"},
  6. widget=forms.widgets.PasswordInput() # 秘文输入
  7. )

单选标签

  1. sex = forms.ChoiceField(
  2. label="性别:",
  3. initial=1, # 默认选择
  4. choices=((1,'男'),(2,'女')),
  5. widget=forms.widgets.RadioSelect()
  6. )

单选下拉框

  1. city = forms.ChoiceField(
  2. label="城市:",
  3. initial=1,
  4. choices=((1,'北京'),(2,'上海')),
  5. widget=forms.widgets.Select()
  6. )

多选

  1. happy = forms.MultipleChoiceField(
  2. label="爱好:",
  3. choices=((1,'打游戏'),(2,'听音乐'),(3,'看书'),(4,'跑步')),
  4. widget=forms.widgets.CheckboxSelectMultiple,
  5. )

多选下拉框

  1. school = forms.MultipleChoiceField(
  2. label="意向学校:",
  3. choices=((1,"北京大学"),(2,"清华大学"),(3,"复旦大学"),(4,"人民大学"),(5,"民族大学")),
  4. widget=forms.widgets.SelectMultiple,
  5. )

单选

  1. status = forms.ChoiceField(
  2. label="是否记住我!",
  3. choices=((True,"是"),(False,"否")),
  4. widget=forms.widgets.CheckboxInput,
  5. )

时间

  1. date = forms.CharField(
  2. label="时间",
  3. widget=forms.widgets.TextInput(attrs={"type":"date"}) # 给标签加属性
  4. )