• FormView用法


    功能描述:

    学生可以对相应学校机构进行投诉建议.

    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='伊妹儿'  
    
  • 相关阅读:
    使用Kuboard界面在k8s上部署SpringCloud项目
    改造项目使用的Dockerfile文件
    在Kuboard上安装 Ingress Controller
    解决nexus仓库只能拉取不能推送的问题
    Logstash:运用 memcache 过滤器进行大规模的数据丰富
    Docker Compose配置文件详解(V3)
    Dockerfile 和 docker-compose.yml的区别
    实战---在Portainer中编排docker-compose.yml文件
    ctk-获取MANIFEST.MF中的数据
    继承时的析构函数
  • 原文地址:https://www.cnblogs.com/tuifeideyouran/p/4233590.html
Copyright © 2020-2023  润新知