zoukankan      html  css  js  c++  java
  • Django-Form 补充

    一、Form的前端循环

               class LoginForm(Form):
                    name = ...
                    pwd = ...
                    
                
                def func(request):
                    form = LoginForm()
                    
                    return ren....
                    
                 //不循环
                <div>
                    {{ form.name }} {{for.errors.name.0}}
                    {{ form.pwd }} {{for.errors.pwd.0}}
                </div>
                //循环
                <div>
                    {% for fd in form %}
                        {{fd.label}}  {{fd}}  {{fd.errors.0}}
                    {% endfor %}
                </div>

    二、自定义Form验证规则

    from正则校验的方法有三种

    
    
    from django.core.exceptions import ValidationError
    from django.core.validators import RegexValidator

    def
    func(val): if len(val) < 15: raise ValidationError('你太短了')#必须是ValidationError class LoginForm(Form): name1 = fields.CharField(label='xxx', widget=widgets.Textarea, validators=[RegexValidator(regex=""),])#第一种 导入 name2 = fields.RegexField(label='xxx', widget=widgets.Textarea, regex="xxx")#第二种 name3 = fields.CharField(label=que.caption, widget=widgets.Textarea, validators=[func, "错误信息"])#第三种 func为上面的func函数 #当然对于一些复杂的逻辑建议使用钩子函数 def clean_name1(self,val): return val

    三 定义类的两种方法

    第一种

    class User(object):
        countury='china'
        def __init__(self,args):
            self.args=args
            
        def get(self):
            return self.args

    第二种

    #类名=type('类名',(继承的类,),dict)
    def ff():
        return 123
    Foo = type('Foo',(object,),{'x1':8,'func':lambda self,arg:arg,"ff":ff})

    应用

    问卷调查学生填写问卷的时候动态生成form

    def fun(value):
        if len(value)<15:
            raise ValidationError("必须超过15字")
    
     dic = {}
        questions=models.Question.objects.filter(questionSuv__id=questionSuv_id).all()
    
        for question in questions:
    
            if question.type==1:
                dic['option_id_%s'%question.id]=fields.ChoiceField(
                   label=question.title,
                   widget=widgets.RadioSelect,
                   choices=models.Option.objects.filter(question=question).values_list("id","title"),
                   error_messages={"required": "必选"},
                 )
            elif question.type ==2:
                dic['value_%s'%question.id]=fields.ChoiceField(
                   label=question.title,
                   widget=widgets.RadioSelect,
                   choices=[(i,i) for i in range(1,11)],
                   error_messages={"required":'必选'},
                )
    
            else:
                dic["content_%s"%question.id]=fields.CharField(
                   label=question.title,
                   widget=widgets.Textarea,
                   error_messages={"required":'必填'},
                   validators=[fun,],
                )
    
    
    
    
        #创建对象的另一种方法 这是动态生成form
        JoinQuestionSuvForm = type('JoinQuestionSuvForm', (Form,), dic)
  • 相关阅读:
    C#-WebApi-EF-CodeFirst-构建迁移命令说明
    webform-rdlc报表报错:未能加载文件或程序集Microsoft.SqlServer.Types版本错误的处理方案
    Observable与Subject
    使用ionic生成apk时遇到的错误:[ERROR] An error occurred while running subprocess cordova
    Socket通讯
    Transactions-事务
    Visual studio2017前端项目包管理从Borwer升级到libman
    NetCore-EFCore-DBfirst-同步数据库生成Model
    学习计划
    关于Vue2.5 less 版本过高
  • 原文地址:https://www.cnblogs.com/ctztake/p/8006251.html
Copyright © 2011-2022 走看看