使用Django Form解决表单数据无法动态刷新的两种方法


Posted in Python onJuly 14, 2017

一、无法动态更新数据的实例

1. 如下,数据库中创建了班级表和教师表,两张表的对应关系为“多对多”

from django.db import models
class Classes(models.Model):
  title = models.CharField(max_length=32)
class Teacher(models.Model):
  name = models.CharField(max_length=32)
  t2c = models.ManyToManyField(Classes)

2. views的功能有查看、添加、编辑班级或教师表

from django.shortcuts import render, redirect
from school import models
from django.forms import Form, fields, widgets
#班级表单验证规则
class ClsForm(Form):
  title = fields.RegexField('老男孩', error_messages={'invalid': '请以 老男孩 开头'})
#教师表单验证规则
class TchForm(Form):
  name = fields.CharField(max_length=16, min_length=2, widget=widgets.TextInput(attrs={'class': 'form-control'}))
  t2c = fields.MultipleChoiceField(
    choices=models.Classes.objects.values_list('id', 'title'),
    widget=widgets.SelectMultiple(attrs={'class': 'form-control'})
  )
#查看班级列表
def classes(request):
  cls_list = models.Classes.objects.all()
  return render(request, 'classes.html', {'cls_list': cls_list})
#查看教师列表
def teachers(request):
  tch_list = models.Teacher.objects.all()
  return render(request, 'teachers.html', {'tch_list': tch_list})
#添加班级
def add_cls(request):
  if request.method == 'GET':
    obj = ClsForm()
    return render(request, 'add_classes.html', {'obj': obj})
  else:
    obj = ClsForm(request.POST)
    if obj.is_valid():
      models.Classes.objects.create(**obj.cleaned_data)
      return redirect('/school/classes/')
    return render(request, 'add_classes.html', {'obj': obj})
#添加教师
def add_tch(request):
  if request.method == 'GET':
    obj = TchForm()
    return render(request, 'add_teacher.html', {'obj': obj})
  else:
    obj = TchForm(request.POST)
    if obj.is_valid():
      tc = obj.cleaned_data.pop('t2c')  # 获取教师任课班级id
      tch_obj = models.Teacher.objects.create(name=obj.cleaned_data['name']) # 添加新教师姓名
      tch_obj.t2c.add(*tc)  # 添加新教师任课班级
      return redirect('/school/teachers/')
    return render(request, 'add_teacher.html', {'obj': obj})
#编辑班级
def edit_cls(request, nid):
  if request.method == 'GET':
    cls = models.Classes.objects.filter(id=nid).first()
    obj = ClsForm(initial={'title': cls.title})
    return render(request, 'edit_classes.html', {'nid': nid, 'obj': obj})
  else:
    obj = ClsForm(request.POST)
    if obj.is_valid():
      models.Classes.objects.filter(id=nid).update(**obj.cleaned_data)
      return redirect('/school/classes/')
    return render(request, 'edit_classes.html', {'nid': nid, 'obj': obj})
#编辑教师
def edit_tch(request, nid):
  if request.method == 'GET':
    tch = models.Teacher.objects.filter(id=nid).first()
    v = tch.t2c.values_list('id')  # 获取该教师任课班级的id
    cls_ids = list(zip(*v))[0] if list(zip(*v)) else []   # 格式化为列表类型
    obj = TchForm(initial={'name': tch.name, 't2c': cls_ids})
    return render(request, 'edit_teacher.html', {'nid': nid, 'obj': obj})
  else:
    obj = TchForm(request.POST)
    if obj.is_valid():
      tc = obj.cleaned_data.pop('t2c')  # 获取修改后的任课班级id
      # models.Teacher.objects.filter(id=nid).update(name=obj.cleaned_data['name'])   # 更新教师姓名方法1
      tch_obj = models.Teacher.objects.filter(id=nid).first()
      tch_obj.name = obj.cleaned_data['name']   # 更新教师姓名方法2
      tch_obj.save()
      tch_obj.t2c.set(tc)
      return redirect('/school/teachers/')
    return render(request, 'edit_teacher.html', {'nid': nid, 'obj': obj})

3. html文件

classe:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>班级列表</title>
  <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
</head>
<body>
<div style="width: 700px; margin: 30px auto">
  <a class="btn btn-default" href="/school/add_cls/" rel="external nofollow" style="margin-bottom: 10px">添加班级</a>
    <table class="table table-hover" border="1" cellspacing="0">
      <thead>
      <tr>
        <th>ID</th>
        <th>班级</th>
        <th>操作</th>
      </tr>
      </thead>
      <tbody>
        {% for item in cls_list %}
          <tr>
            <td>{{ item.id }}</td>
            <td>{{ item.title }}</td>
            <td><a href="/school/edit_cls/{{ item.id }}" rel="external nofollow" >编辑</a></td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
</div>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>添加班级</title>
</head>
<body>
<h1>添加班级</h1>
<form action="/school/add_cls/" method="post">
  {% csrf_token %}
  <p>
    {{ obj.title }} {{ obj.errors.title.0 }}
  </p>
  <input type="submit" value="提交">
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>编辑班级</title>
</head>
<body>
<h1>编辑班级</h1>
<form action="/school/edit_cls/{{ nid }}" method="post">
  {% csrf_token %}
  <p>
    {{ obj.title }} {{ obj.errors.title.0 }}
  </p>
  <input type="submit" value="提交">
</form>
</body>
</html>

 teachers:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>教师列表</title>
  <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
</head>
<body>
<div style="width: 700px; margin: 30px auto">
  <a class="btn btn-default" href="/school/add_tch/" rel="external nofollow" style="margin-bottom: 10px">添加教师</a>
    <table class="table table-hover" border="1" cellspacing="0">
      <thead>
      <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>任教班级</th>
        <th>操作</th>
      </tr>
      </thead>
      <tbody>
        {% for item in tch_list %}
          <tr>
            <td>{{ item.id }}</td>
            <td>{{ item.name }}</td>
            <td>
              {% for row in item.t2c.all %}
                <span style="border: solid gray 1px">{{ row.title }}</span>
              {% endfor %}
            </td>
            <td><a href="/school/edit_tch/{{ item.id }}" rel="external nofollow" >编辑</a></td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>添加教师</title>
  <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
</head>
<body>
<div style="width: 500px; margin: 20px auto">
<h3 style="width: 100px; margin: 10px auto">添加教师</h3>
  <form class="form-horizontal" action="/school/add_tch/" method="post">
    {% csrf_token %}
 <div class="form-group">
  <label class="col-sm-2 control-label">姓名</label>
  <div class="col-sm-10">
   {{ obj.name }} {{ obj.errors.name.0 }}
  </div>
 </div>
 <div class="form-group">
  <label class="col-sm-2 control-label">班级</label>
  <div class="col-sm-10">
      {{ obj.t2c }} {{ obj.errors.t2c.0 }}
  </div>
 </div>
 <div class="form-group">
  <div class="col-sm-offset-2 col-sm-10">
   <input type="submit" class="btn btn-default" value="提交"></input>
  </div>
 </div>
</form>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>编辑教师</title>
  <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
</head>
<body>
<div style="width: 500px; margin: 20px auto">
<h3 style="width: 100px; margin: 10px auto">编辑教师</h3>
  <form class="form-horizontal" action="/school/edit_tch/{{ nid }}" method="post">
    {% csrf_token %}
 <div class="form-group">
  <label class="col-sm-2 control-label">姓名</label>
  <div class="col-sm-10">
   {{ obj.name }} {{ obj.errors.name.0 }}
  </div>
 </div>
 <div class="form-group">
  <label class="col-sm-2 control-label">班级</label>
  <div class="col-sm-10">
      {{ obj.t2c }} {{ obj.errors.t2c.0 }}
  </div>
 </div>
 <div class="form-group">
  <div class="col-sm-offset-2 col-sm-10">
   <input type="submit" class="btn btn-default" value="提交"></input>
  </div>
 </div>
</form>
</div>
</body>
</html>

4. 数据不能同步

在班级表中新增一条记录

使用Django Form解决表单数据无法动态刷新的两种方法

在教师表中新添加一名教师,发现无法获取上一步新增记录

使用Django Form解决表单数据无法动态刷新的两种方法

5. 原因分析

在添加教师时,请求方式为GET,html标签由Form组件自动生成,其中的数据也是由Form组件提供

使用Django Form解决表单数据无法动态刷新的两种方法

而TchForm作为一个类,在project运行起来后,其中的name和t2c字段都是类的变量,其只执行一次,就将数据保存在内存中,无论之后生成多少个TchForm对象,其中的字段的值都不变。

所以会出现教师表中的班级多选列表无法动态更新。

使用Django Form解决表单数据无法动态刷新的两种方法

二、解决上述bug的方法

每次更新数据库后重启project,让Form类重新初始化,能够让数据更新,但这显然是不切实际的。

知道了bug的根源,我们可以尝试让每次生成TchForm对象时就更新数据:

方法一

1. 利用 __init__将数据库操作放入对象变量中

 修改TchForm类

#教师表单验证规则
class TchForm(Form):
  name = fields.CharField(max_length=16, min_length=2, widget=widgets.TextInput(attrs={'class': 'form-control'}))
  t2c = fields.MultipleChoiceField(
    # choices=models.Classes.objects.values_list('id', 'title'),
    widget=widgets.SelectMultiple(attrs={'class': 'form-control'})
  )
  def __init__(self, *args, **kwargs):  # 自定义__init__
    super(TchForm, self).__init__(*args, **kwargs) # 调用父类的__init__
    self.fields['t2c'].choices = models.Classes.objects.values_list('id', 'title')  # 为字段t2c的choices赋值

2. 验证

 在班级表中新增一条记录

使用Django Form解决表单数据无法动态刷新的两种方法

 再在教师表中添加

使用Django Form解决表单数据无法动态刷新的两种方法

方法二

1. 利用django.forms.models模块中的queryset连接数据库

 修改TchForm类

#教师表单验证规则
from django.forms import models as form_models # 导入django.forms.models
class TchForm(Form):
  name = fields.CharField(max_length=16, min_length=2, widget=widgets.TextInput(attrs={'class': 'form-control'}))
  #重新定义字段
  t2c = form_models.ModelMultipleChoiceField(
    # choices=models.Classes.objects.values_list('id', 'title'),
    queryset=models.Classes.objects.all(), # 利用queryset连接数据库,只能连接object类型
    widget=widgets.SelectMultiple(attrs={'class': 'form-control'})
  )

2. 验证

由于TchForm类中,queryset只能连接object类型,所以,需要设置models.py中的Classes类的返回值。

 设置models.py中的Classes类的返回值

class Classes(models.Model):
  title = models.CharField(max_length=32)
   def __str__(self):
     return self.title

在班级表中新增一条记录

使用Django Form解决表单数据无法动态刷新的两种方法

再在教师表中添加

使用Django Form解决表单数据无法动态刷新的两种方法

以上所述是小编给大家介绍的使用Django Form解决表单数据无法动态刷新的两种方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
wxpython中利用线程防止假死的实现方法
Aug 11 Python
Python字符串特性及常用字符串方法的简单笔记
Jan 04 Python
python去除文件中空格、Tab及回车的方法
Apr 12 Python
利用ctypes提高Python的执行速度
Sep 09 Python
python实现校园网自动登录的示例讲解
Apr 22 Python
Python干货:分享Python绘制六种可视化图表
Aug 27 Python
Python多进程方式抓取基金网站内容的方法分析
Jun 03 Python
Django自定义用户登录认证示例代码
Jun 30 Python
pytorch构建多模型实例
Jan 15 Python
pytorch方法测试详解——归一化(BatchNorm2d)
Jan 15 Python
python shapely.geometry.polygon任意两个四边形的IOU计算实例
Apr 12 Python
使用pth文件添加Python环境变量方式
May 26 Python
Python md5与sha1加密算法用法分析
Jul 14 #Python
Python自动化开发学习之三级菜单制作
Jul 14 #Python
python实现杨辉三角思路
Jul 14 #Python
Django 添加静态文件的两种实现方法(必看篇)
Jul 14 #Python
python 实现上传图片并预览的3种方法(推荐)
Jul 14 #Python
Python加密方法小结【md5,base64,sha1】
Jul 13 #Python
利用Python实现Windows下的鼠标键盘模拟的实例代码
Jul 13 #Python
You might like
PHP实现自动识别Restful API的返回内容类型
2015/02/07 PHP
使用Git实现Laravel项目的自动化部署
2019/11/24 PHP
JS上传图片前的限制包括(jpg jpg gif及大小高宽)等
2012/12/19 Javascript
ff下JQuery无法监听input的keyup事件的解决方法
2013/12/12 Javascript
javascript中call和apply的用法示例分析
2015/04/02 Javascript
js实现Form栏显示全格式时间时钟效果代码
2015/08/19 Javascript
js实现继承的5种方式
2015/12/01 Javascript
js无法获取到html标签的属性的解决方法
2016/07/26 Javascript
各式各样的导航条效果css3结合jquery代码实现
2016/09/17 Javascript
纯js三维数组实现三级联动效果
2017/02/07 Javascript
详解基于Bootstrap+angular的一个豆瓣电影app
2017/06/26 Javascript
Angular实现点击按钮后在上方显示输入内容的方法
2017/12/27 Javascript
vue和webpack打包项目相对路径修改的方法
2018/06/15 Javascript
解决layui 三级联动下拉框更新时回显的问题
2019/09/03 Javascript
vue+element导航栏高亮显示的解决方式
2019/11/12 Javascript
[00:31]2016完美“圣”典风云人物:国士无双宣传片
2016/12/04 DOTA
[42:24]完美世界DOTA2联赛循环赛 LBZS vs DM BO2第一场 11.01
2020/11/02 DOTA
Python实现的下载8000首儿歌的代码分享
2014/11/21 Python
python创建临时文件夹的方法
2015/07/06 Python
Python实现类似比特币的加密货币区块链的创建与交易实例
2018/03/20 Python
django模板结构优化的方法
2019/02/28 Python
解决Python3 抓取微信账单信息问题
2019/07/19 Python
Django 静态文件配置过程详解
2019/07/23 Python
python实现对服务器脚本敏感信息的加密解密功能
2019/08/13 Python
Python调用.NET库的方法步骤
2019/12/27 Python
Python列表解析操作实例总结
2020/02/26 Python
如何用python 操作zookeeper
2020/12/28 Python
详解canvas绘图时遇到的跨域问题
2018/03/22 HTML / CSS
澳大利亚冲浪和时尚服装网上购物:SurfStitch
2017/07/29 全球购物
英国家电购物网站:Sonic Direct
2019/03/26 全球购物
Yahoo-PHP面试题4
2012/05/05 面试题
WEB控件可以激发服务端事件,请谈谈服务端事件是怎么发生并解释其原理?自动传回是什么?为什么要使用自动传回?
2012/02/21 面试题
农村婚礼证婚词
2014/01/08 职场文书
公务员综合考察材料
2014/02/01 职场文书
批评与自我批评发言稿
2014/10/15 职场文书
JavaWeb 入门篇:创建Web项目,Idea配置tomcat
2021/07/16 Java/Android