django 添加动态表格的方法
传统方法(基于方法的视图):http://stellarchariot.com/blog/2011/02/dynamically-add-form-to-formset-using-javascript-and-django/
概要:
服务器端,使用了formset , 文档在这里:https://docs.djangoproject.com/en/dev/topics/forms/formsets/
客户端,使用脚本动态添加内容。
class based view ,参考这里:http://kevindias.com/writing/django-class-based-views-multiple-inline-formsets/
总结:
重写了get/post方法。
要点:
1. form里面做关联:
# forms.py
from django.forms import ModelForm
from django.forms.models import inlineformset_factory from .models import Recipe, Ingredient, Instruction class RecipeForm(ModelForm):
class Meta:
model = Recipe IngredientFormSet = inlineformset_factory(Recipe, Ingredient)
InstructionFormSet = inlineformset_factory(Recipe, Instruction)
2. 重写post/get方法,并在里面对子表初始化,注意get里面构造时无参,post里有参。
ingredient_form = IngredientFormSet() vs ingredient_form = IngredientFormSet(self.request.POST)
class RecipeCreateView(CreateView):
template_name = 'recipe_add.html'
model = Recipe
form_class = RecipeForm
success_url = 'success/' def get(self, request, *args, **kwargs):
"""
Handles GET requests and instantiates blank versions of the form
and its inline formsets.
"""
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
ingredient_form = IngredientFormSet()
instruction_form = InstructionFormSet()
return self.render_to_response(
self.get_context_data(form=form,
ingredient_form=ingredient_form,
instruction_form=instruction_form)) def post(self, request, *args, **kwargs):
"""
Handles POST requests, instantiating a form instance and its inline
formsets with the passed POST variables and then checking them for
validity.
"""
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
ingredient_form = IngredientFormSet(self.request.POST)
instruction_form = InstructionFormSet(self.request.POST)
if (form.is_valid() and ingredient_form.is_valid() and
instruction_form.is_valid()):
return self.form_valid(form, ingredient_form, instruction_form)
else:
return self.form_invalid(form, ingredient_form, instruction_form)
3. 保存时做关联:
ingredient_form.instance = self.object
def form_valid(self, form, ingredient_form, instruction_form):
"""
Called if all forms are valid. Creates a Recipe instance along with
associated Ingredients and Instructions and then redirects to a
success page.
"""
self.object = form.save()
ingredient_form.instance = self.object
ingredient_form.save()
instruction_form.instance = self.object
instruction_form.save()
return HttpResponseRedirect(self.get_success_url())
4. 模板注意包含两个隐藏域:
{{ ingredient_form.management_form }}
{{ ingredient_form.non_form_errors }}
3. 自己写了个demo,完整代码看这里: https://github.com/TommyU/dynamic_form/
django 添加动态表格的方法的更多相关文章
- 在<s:iterator>标签里给动态表格添加序号
在<s:iterator>标签里给动态表格添加序号,需要用到<s:iterator>标签里的Status属性里的count eg:<s:iterator value=&q ...
- iOS中动态注入JavaScript方法。动态给html标签添加事件
项目中有这样一种需求,给html5网页中图片添加点击事件,并且弹出弹出点击的对应的图片,并且可以保持图片到本地 应对这样的需求你可能会想到很多方法来实现. 1. 最简单的方法就是在html5中添加图片 ...
- QT中添加 动态库(.so) 和 静态库 (.a) 的方法
在QT 的Makefile文件中: 1 添加动态库,如lipcap.so 则,在LIBS一行中添加“-L/usr/local/lib -lpcap”,依据自己的情况修改libpcap.so的路径 2 ...
- excel表格中添加单引号的方法
今天碰到需要插入大量数据的excel表格,其中有很多文本,需要添加单引号. 方法如下: 左边是原始数据,右边是我即将添加单引号的空白区域. 第一步:在需要添加的位置输入= 第二步:输入等号之后点击需要 ...
- python装饰器、继承、元类、mixin,四种給类动态添加类属性和方法的方式(一)
介绍装饰器.继承.元类.mixin,四种給类动态添加类属性和方法的方式 有时候需要給类添加额外的东西,有些东西很频繁,每个类都需要,如果不想反复的复制粘贴到每个类,可以动态添加. # coding=u ...
- js动态给table添加/删除tr的方法
js动态给table添加/删除tr的方法. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ...
- Celery 分布式任务队列快速入门 以及在Django中动态添加定时任务
Celery 分布式任务队列快速入门 以及在Django中动态添加定时任务 转自 金角大王 http://www.cnblogs.com/alex3714/articles/6351797.html ...
- C#后台动态添加Grid表格
前面页面: <ScrollViewer x:Name=" BorderBrush="#25A0DA" VerticalScrollBarVisibility=&qu ...
- django在style的样式image url添加静态图片路径和django如何动态传入图片链接?
#django在style的样式image url添加静态图片路径 style=" background:url({% static "agribusiness/images/lo ...
随机推荐
- Nvidia Nsight + .NET
https://devtalk.nvidia.com/default/topic/804306/nsight-4-5-can-t-debug-net-applications/ http://comm ...
- yourphp基本语句
实例化页面代码 1.时间代码:{$vo.createtime|toDate=###,'Y-m-d H:i:s'} 2.连接:{:U('Pro/arr')},{:URL()} 如:<form ac ...
- 其他系统与ecshop的会员整合
步骤一:整合两个的会员数据 用软件Navicat 的 "导入向导"功能,导入你的原数据类型(sql,mdb,db)我的是mdb类型.下一步选择你原有的会员字段“user”.再进行下 ...
- JAVA JLabel自定义子类无法显示
import java.awt.*; import java.util.Scanner; import javax.swing.*; public class Test_16_13 extends J ...
- trace enabled
<system.web> <trace enabled="true" localOnly="true" pageOutput="tr ...
- 单选框的回显c:if
<input type="radio" name="sex" value="boy" <c:if test="${te ...
- [Js/Jquery]table行转列
摘要 在使用ews调用exhange的收件箱的并在h5页面显示邮件详情的时候,因为返回的每封邮件的内容都是htmlbody,没有textbody.每封邮件又没什么规律,用正则表达式来匹配内容并不合适, ...
- xml基础总结
可扩展的标记语言(eXtensible Markup Language) 优点:容易读懂:格式标准任何语言都内置了XML分析引擎,不用单独进行文件分析引擎的编写. 用普通二进制传输数据的缺点,解析方式 ...
- jsp应用
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 分享php中四种webservice实现的简单架构方法及实例
一:PHP本身的SOAP所有的webservice都包括服务端(server)和客户端(client).要使用php本身的soap首先要把该拓展安装好并且启用.下面看具体的code首先这是服务端实现: ...