#模板
class IndexForm(forms.Form):
# 模板,用户提交的name和这里的变量名一定要是一致的.否则不能获取数据
user = forms.CharField(min_length=6, error_messages={'required': '用户名不能为空', 'min_length': '用户名长度不能小于6'})
email = forms.EmailField(error_messages={'required': '邮箱不能为空', 'invalid': '邮箱格式错误'})
"""
单选
favor = forms.ChoiceField(
choices=[(1, '小虎'), (2, '小小虎'), (3, '小B虎')]
)
"""
# 多选
favor = forms.MultipleChoiceField(
choices=[(1, '小虎'), (2, '小小虎'), (3, '小B虎')]
)
pass
#函数
def index(request):
obj = IndexForm()
return render(request, "index.html", {'obj': obj})
def edit_index(request):
obj = IndexForm({'user': 'root', 'email': '123@qq.com', 'favor': [2,3]})
return render(request, "index.html", {'obj': obj})
#前端
Title
{{ obj.user }}
{{ obj.email }}
{{ obj.favor }}