Django:在没有 javascript 的情况下添加内联表单集行 [英] Django: Adding inline formset rows without javascript

查看:27
本文介绍了Django:在没有 javascript 的情况下添加内联表单集行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章与此相关:在 django admin 中动态添加行到内联

有没有办法在不使用 javascript 的情况下实现添加内联表单集?显然,这会涉及到页面刷新.

Is there a way to achive adding inline formsets WITHOUT using javascript? Obviously, there would be a page-refresh involved.

因此,如果表单有一个名为添加"的按钮...

So, if the form had a button called 'add'...

我想我可以这样做:

if request.method=='POST':
  if 'add' in request.POST:
    PrimaryFunctionFormSet = inlineformset_factory(Position,Function,extra=1)
    prims = PrimaryFunctionFormSet(request.POST)

我认为每次都会加 1,然后用帖子数据填充表单.不过好像extra=1并没有给post数据加1.

Which I thought would add 1 each time, then populate the form with the post data. However, it seems that the extra=1 does not add 1 to the post data.

推荐答案

知道了.

有时这是最简单的解决方案.只需复制 request.POST 数据并修改 TOTAL-FORMS.

Sometimes it's the simplest solution. Just make a copy of the request.POST data and modify the TOTAL-FORMS.

例如..

if request.method=='POST':
  PrimaryFunctionFormSet = inlineformset_factory(Position,Function)
  if 'add' in request.POST:
    cp = request.POST.copy()
    cp['prim-TOTAL_FORMS'] = int(cp['prim-TOTAL_FORMS'])+ 1
    prims = PrimaryFunctionFormSet(cp,prefix='prim')

然后像往常一样吐出表格.保留您的数据,添加内联编辑器.

Then just spit the form out as normal. Keeps your data, adds an inline editor.

这篇关于Django:在没有 javascript 的情况下添加内联表单集行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆