如何将数组发布到views.py [英] How to post a array to views.py

查看:151
本文介绍了如何将数组发布到views.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {item for ...在产品%} 

< div class =register_div>


< p>< label for =id [{item.id}}]> {{item.Name}}< / label> < input type =textname =custom [{item.id}}]/>< / p>

< / div>

{%endfor%}

现在在我的意见中,我想将此数据保存到我的数据库。但是首先我检查了我的数组是否返回某些东西。我只是尝试打印为。

  q = upload_form.data ['custom []'] 

  q = upload_form.data ['custom'] 

但它给我这个错误

 Key'custom [] '** OR ** key custom not found in< QueryDict:{u'custom [2]':[u's'],u'custom [1]':[u'a'],u'price':[你的'u''标题':[u]',u'customatt':[u,u],u'csrfmiddlewaretoken':[u'up4Ipd5L13Efk14MI3ia2FRYScMwMJLz']}> 

但如果我打印此

  q = upload_form.data ['custom [1]'] 

那么它显示数组1的值。



所以请建议我一个更好的方法来做到这一点,我可以在我的数组中显示在views.py

解决方案

由于 upload_form.data 是一个字典,关键是'custom []'根本不存在尝试一下:

  custom_values = {} 
为key,在upload_form.data.items()中的值:
如果key.startswith('custom'):
custom_values [key] = value

字典 custom_values 现在拥有您所有的自定义表单值。


I am using for loop in my template and for different id of each record i am using arrays

{% for item in product %}

<div class="register_div">


    <p><label for="id[{{ item.id }}]">{{ item.Name }} </label> <input type="text" name="custom[{{item.id}}]"/></p>

</div>

{% endfor %}

Now when in my views i want to save this data to my database.But firstly i checked that whether my array return something or not.So i just try print that as.

q = upload_form.data['custom[]']

or

 q = upload_form.data['custom']

but it gives me this error

"Key 'custom[]' **OR** key custom not found in <QueryDict: {u'custom[2]': [u's'], u'custom[1]': [u'a'], u'price': [u''], u'title': [u''], u'customatt': [u'', u''], u'csrfmiddlewaretoken': [u'up4Ipd5L13Efk14MI3ia2FRYScMwMJLz']}>"

but if i print this

q = upload_form.data['custom[1]']

then it display the value of array 1.

so please suggest me a better way to do this how i can display all values inside my array in views.py

解决方案

As upload_form.data is a dictionary, the key 'custom[]' simply doesn't exist. Try something like:

custom_values = {}    
for key, value in in upload_form.data.items():
    if key.startswith('custom'):
        custom_values[key]=value

The dictionary custom_values holds now all your 'custom' form values.

这篇关于如何将数组发布到views.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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