Django:添加没有javascript的内联formset行 [英] Django: Adding inline formset rows without javascript

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

问题描述

这篇文章涉及到:
将行添加到在django管理员中动态嵌入



有没有办法在不使用javascript的情况下添加内联表单?显然,会涉及一个页面刷新。



所以,如果表单有一个名为add的按钮...



我想我可以这样做:

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

我以为每次添加1,然后填写表单与帖子数据。然而,似乎extra = 1并没有为post数据添加1。

解决方案



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



例如..

  if request.method =='POST':
PrimaryFunctionFormSet = inlineformset_factory(Position,Function)
如果在request.POST中添加:P $ = request.POST.copy()
cp ['prim-TOTAL_FORMS'] = int(cp ['prim-TOTAL_FORMS'])+ 1
prims = PrimaryFunctionFormSet(cp,prefix ='prim')

然后只要正常地吐出表单。保存数据,添加内联编辑器。


This post relates to this: Add row to inlines dynamically in django admin

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'...

I figured I could do it like this:

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

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.

解决方案

Got it.

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

for example..

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的内联formset行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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