功能描述:
学生可以对相应学校机构进行投诉建议.
form表单
class SuggestForm(forms.Form):
TYPE_CHOICES = (
(0, u'学校'),
(1, u'学院'),
(2, u'校院'),
)
content = forms.CharField(label=u"内容", required=True, widget=forms.Textarea(attrs={'cols': 33}),
error_messages={'required': u'请留下你的建议哦'})
type = forms.ChoiceField(label=u"类型", required=True, widget=forms.RadioSelect, choices=TYPE_CHOICES,
error_messages={'required': u'请做出你的选择哦'})
error_messages = {
'content': {
'required': "内容不能为空哦"
},
} def save(self, user, commit=True):
cleaned_data = super(SuggestForm, self).clean()
student = Student.objects.get(user=user)
complain = Complain(student=student, content=cleaned_data.get("content"))
complain.save()
前台显示
<form action="" method="post">
{% csrf_token %}
{{ form.as_table }}
<input type="submit" value="提交" class="ahref"/>
</form>
view定义
from django.views.generic import FormView
class SuggestView(FormView): template_name = "student/suggest.html"
form_class = SuggestForm def form_valid(self, form):
form.save(self.request.user)
return render(self.request, self.template_name, {"success": u"提交建议成功"})
#===如果需要变更样式, 或者变更显示中文名称.更改 label
def __init__(self, *args, **kwargs):
super(TestModelForm, self).__init__(*args, **kwargs)
#====改变样式,也可以赋值 class=???,在外面html页面上先定义好,个人不推荐直接在代码里写,只是为了演示。
self.fields['yourname'].widget.attrs.update({'style' : 'border:1px dashed #ccc;'})
self.fields['email'].label='伊妹儿'