使用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 相关文章推荐
Python常见文件操作的函数示例代码
Nov 15 Python
Python调用C语言开发的共享库方法实例
Mar 18 Python
使用url_helper简化Python中Django框架的url配置教程
May 30 Python
Collatz 序列、逗号代码、字符图网格实例
Jun 22 Python
让Django支持Sql Server作后端数据库的方法
May 29 Python
python3实现字符串的全排列的方法(无重复字符)
Jul 07 Python
python实现录音小程序
Oct 26 Python
初探利用Python进行图文识别(OCR)
Feb 26 Python
Python+OpenCv制作证件图片生成器的操作方法
Aug 21 Python
Python求平面内点到直线距离的实现
Jan 19 Python
python环境下安装opencv库的方法
Mar 05 Python
Python爬虫之爬取哔哩哔哩热门视频排行榜
Apr 28 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
德生PL330测评
2021/03/02 无线电
php 调试利器debug_print_backtrace()
2012/07/23 PHP
PHP使用CURL实现对带有验证码的网站进行模拟登录的方法
2014/07/23 PHP
laravel5.4利用163邮箱发送邮件的步骤详解
2017/09/22 PHP
js 格式化时间日期函数小结
2010/03/20 Javascript
javascript 通用loading动画效果实例代码
2014/01/14 Javascript
JS动态修改iframe高度和宽度的方法
2015/04/01 Javascript
学习JavaScript设计模式之策略模式
2016/01/12 Javascript
手机端 HTML5使用photoswipe.js仿微信朋友圈图片放大效果
2016/08/25 Javascript
Extjs gridpanel 中的checkbox(复选框)根据某行的条件不能选中的解决方法
2017/02/17 Javascript
vue组件之Alert的实现代码
2017/10/17 Javascript
移动端效果之IndexList详解
2017/10/20 Javascript
jQuery实现的简单手风琴效果示例
2018/08/29 jQuery
vue项目动态设置页面title及是否缓存页面的问题
2018/11/08 Javascript
AngularJS上传文件的示例代码
2018/11/10 Javascript
js常用正则表达式集锦
2019/05/17 Javascript
[11:01]2014DOTA2西雅图邀请赛 冷冷带你探秘威斯汀
2014/07/08 DOTA
Python入门及进阶笔记 Python 内置函数小结
2014/08/09 Python
Python中用pycurl监控http响应时间脚本分享
2015/02/02 Python
Python制作豆瓣图片的爬虫
2017/12/28 Python
Python 通过调用接口获取公交信息的实例
2018/12/17 Python
pandas进行时间数据的转换和计算时间差并提取年月日
2019/07/06 Python
Python 保持登录状态进行接口测试的方法示例
2019/08/06 Python
Python 解析pymysql模块操作数据库的方法
2020/02/18 Python
对python中list的五种查找方法说明
2020/07/13 Python
python selenium xpath定位操作
2020/09/01 Python
CSS3打造百度贴吧的3D翻牌效果示例
2017/01/04 HTML / CSS
Farah官方网站:男士服装及配件
2019/11/01 全球购物
What is the purpose of Void class? Void类的作用是什么?
2016/10/31 面试题
安全生产大检查方案
2014/05/07 职场文书
小学感恩节活动策划方案
2014/10/06 职场文书
财务会计实训报告
2014/11/05 职场文书
2015年中学元旦晚会活动方案
2014/12/09 职场文书
优秀大学生自荐信
2015/03/26 职场文书
小学主题班会教案
2015/08/17 职场文书
《槐乡的孩子》教学反思
2016/02/20 职场文书