一、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)

Django-Form 补充的更多相关文章

  1. day20 FORM补充(随时更新),F/Q操作,model之多对多,django中间件,缓存,信号

    python-day20 1.FROM生成select标签的数据应该来源于数据库. 2.model 操作 F/Q  (组合查询) 3.model 多对多操作. 4.中间件 :在请求到达url前先会经过 ...

  2. django form表单验证

    一. django form表单验证引入 有时时候我们需要使用get,post,put等方式在前台HTML页面提交一些数据到后台处理例 ; <!DOCTYPE html> <html ...

  3. Django form表单

    Form介绍 之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来.与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否输入, ...

  4. Django Form和ModelForm组件

    Form介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否 ...

  5. Python学习---django知识补充之CBV

    Django知识补充之CBV Django: url    -->  def函数      FBV[function based view]  用函数和URL进行匹配 url    --> ...

  6. Django学习笔记之Django Form表单详解

    知识预览 构建一个表单 在Django 中构建一个表单 Django Form 类详解 使用表单模板 回到顶部 构建一个表单 假设你想在你的网站上创建一个简单的表单,以获得用户的名字.你需要类似这样的 ...

  7. Django学习笔记之Django Form表单

    Form介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否 ...

  8. Django Form and ModelForm

    Form介绍 在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否输入,输 ...

  9. 6月28日 Django form组件 和 modelform组件

    Form介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否 ...

  10. django: form fileupload - 1

    本节介绍 Form 中一些字段类型的使用,以文件上传字段 FileField 为例:(注,其它字段和相关用法见官方文档中的 Forms -> Built-in Fields) 一,配置 urls ...

随机推荐

  1. Leetcode. 回文字符串的分割和最少分割数

    Q1: 回文字符串的分割 Given a string s, partition s such that every substring of the partition is a palindrom ...

  2. 为Zabbix配置Nova服务、Keystone和Placement进程CPU和内存usage监控

    目前已经完成了RabbitMQ和MySQL的监控项配置,还差对nova-api.nova-conductor.nova-scheduler和keystone进程CPU和内存 usage的监控,类似的轮 ...

  3. Python 进阶(一些进阶技巧)

    个人笔记,基本都摘抄自 Python3 官方文档 一. 上下文管理 1. 传统的类方式 Java 使用 try 来自动管理资源,只要实现了 AutoCloseable 接口,就可以部分摆脱手动 col ...

  4. eniac世界第二台计算机

    ENIAC,全称为Electronic Numerical Integrator And Computer,即电子数字积分计算机.ENIAC是世界上第一台通用计算机,也是继ABC(阿塔纳索夫-贝瑞计算 ...

  5. online community

    online community spectrum https://spectrum.chat/xgqfrms https://community.xgqfrms.xyz/ https://spect ...

  6. 安装与配置JDK

    第一步:下载jdk-7-linux-i586.tar.gzwget -c http://download.oracle.com/otn-pub/java/jdk/7/jdk-7-linux-i586. ...

  7. Luogu 3435 POI2006OKR-Periods of Words(kmp)

    显然答案应该是Σi-next[next[……next[i]]] (next[next[……next[i]]]>0).递推即可. #include<iostream> #include ...

  8. perl的Sys::Syslog模块(openlog,syslog,closelog函数,setlogsock)-自定义日志

    perl的Sys::Syslog模块(openlog,syslog,closelog函数,setlogsock)-自定义日志 http://blog.chinaunix.net/xmlrpc.php? ...

  9. [Leetcode] Recover binary search tree 恢复二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  10. How to reclaim space in InnoDB when innodb_file_per_table is ON

    When innodb_file_per_table is OFF and all data is going to be stored in ibdata files. If you drop so ...