动态添加的表单域在form.cleaned_data中被删除 [英] Dynamically added form fields are removed in form.cleaned_data

查看:91
本文介绍了动态添加的表单域在form.cleaned_data中被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一些客户端Javascript放在我的模板中,允许用户动态地向表单添加字段。我的问题是这些字段在 form.cleaned_data 中清理,所以我无法访问它们。



所有的字段都可以在 request.POST 中访问,所以我可以解决这个问题,但是我想这样做是正确的方式,我认为解决方案在于使用django表单,而不是直接阅读请求。



我尝试覆盖 form.clean() ,但似乎数据已经到了那里。



其他细节:我命名这些字段 fieldname_x ,其中 x 是一个数字。在 request.POST request.POST ['fieldname'] 是一个所有值的列表,但 form.cleaned_data 仅包含每个列表的最后一个值。

解决方案

你知道这些领域是什么类型的事情?他们只是简单的文字领域吗?我已经做了类似的事情,创建动态表单。

 #确保这些存在通过检查request.POST 
custom_fields = ['fieldname_1','fieldname_2']

attrs = dict((field,forms.CharField(max_length = 100,required = False))
for custom_fields中的字段
DynamicForm = type(DynamicForm,(YourBaseForm,),attrs)
submitted_form = DynamicForm(request.POST)

您提交的表单现在应包含所需的所有字段及其值。您可能需要删除required = False,但这取决于您。



这是什么,执行基本形式的动态子类,将传入的属性添加为attrs到类的定义。因此,当您使用帖子数据创建实例时,应正确映射。



编辑:



我阅读这个问题更加紧密。您要做的是确保您的动态输入元素的名称正确,并且值达到django后,这些值将映射到这些字段名称。否则,request.POST将不会正确填写表单。

 < input type ='text'name ='fieldname_1' value ='value_for_field_1'/> 


I put some client-side Javascript in my template that allows a user to dynamically add fields to a form. My problem is that these fields are cleaned in form.cleaned_data, so I can't access them that way.

All the fields are accessible in request.POST, so I could just solve this problem with that, but I want to do this the "right way" and I think that the solution lies somewhere in using django forms rather than reading the request directly.

I tried overriding form.clean(), but it seems like the data is already gone by the time it gets there.

Other details: I am naming these fields fieldname_x, where x is a number. In request.POST, request.POST['fieldname'] is a list of a all the values, but form.cleaned_data contains only the last value of each list.

解决方案

Do you know what type these fields are going to be beforehand? Are they just going to be simple text fields? I've done something similar to this, creating dynamic forms.

# make sure these exist by examining request.POST
custom_fields = ['fieldname_1', 'fieldname_2']

attrs = dict((field, forms.CharField(max_length=100, required=False)) 
             for field in custom_fields)
DynamicForm = type("DynamicForm", (YourBaseForm,), attrs)
submitted_form = DynamicForm(request.POST)

Your submitted form should now contain all the fields you require, along with their values. You might want to remove required=False, but that's up to you.

What this does, is perform a dynamic subclass of your base form, adding the attributes passed in as attrs to the class definition. So when you create an instance with post data, they should be mapped correctly.

Edit:

I read the question a little more closely. What you'll want to do is ensure that your dynamic input elements are named correctly, and the values map to those fieldnames once it reaches django. Otherwise, request.POST will not fill the form in correctly.

<input type='text' name='fieldname_1' value='value_for_field_1' /> 

etc

这篇关于动态添加的表单域在form.cleaned_data中被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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