Django和HTML数组 [英] Django and HTML arrays
问题描述
我有一个表单与这个输入:
< input name =person [name]value = >
< input name =person [surname]value =>
< input name =person [age]value =>
当我提交,我如何将该html数组分配给一个变量,因为request.POST.getlist ('person')不工作,我一直在检查其他帖子,但是我发现唯一没有任何有用的东西
我希望有人可以帮我弄清楚,因为阅读文档,并且做得很顺利...
事实是,我有一个桌子在我的数据分析中有折扣...每个折扣都有一个默认值...所以我做了
discounts = Discount.objects.all()
{%for i in discounts%}
< input name =discount [{{i.id} }]value ={{i.default_value}}>
{%endfor%}
在我没有任何方法来捕获该HTML数组我发送...任何建议?
对不起,回答这样一个老问题,但我坚持一样问题,没有找到任何可接受的答案。所以,这是我的解决方案:
def get_post_dict(post,key):
result = {}
如果发布:
import re
patt = re.compile('^([a-zA-Z_] \w +)\ [([a-zA-Z_\ - ] [\\ $'$')
for post_name,post.items()中的值:
value = post [post_name]
match = patt.match(post_name )
如果不匹配或不是值:
continue
name = match.group(1)
如果name == key:
k = match.group(2)
result.update({k:value})
返回结果
现在你可以这样使用:
persons = get_post_dict(request.POST,'person')
...
或
django.http.QueryDict.getdict = get_post_dict
persons = request.POST.getdict('person')
I have a form with this inputs:
<input name="person[name]" value="">
<input name="person[surname]" value="">
<input name="person[age]" value="">
when I submit, how can i assign that html array to a variable, cause request.POST.getlist('person') doesn't work, i been checking for other post but the only one i found doesn't have anything usefull
I hope someone could help me figure it out, cause a read the doc, and did quite get the way to do it...
the thing is that i have a table in my db with discounts... where every discount has a default value... so i made it
discounts = Discount.objects.all( )
{% for i in discounts %}
<input name="discount[{{ i.id }}]" value="{{ i.default_value }}">
{% endfor %}
and on my i dont have any method to catch that html array i'm sending... any suggestions?
Sorry for answering such an old question, but i stuck with the same problem and didn't found any acceptable answers for it. So, here is my solution:
def get_post_dict(post, key):
result = {}
if post:
import re
patt = re.compile('^([a-zA-Z_]\w+)\[([a-zA-Z_\-][\w\-]*)\]$')
for post_name, value in post.items():
value = post[post_name]
match = patt.match(post_name)
if not match or not value:
continue
name = match.group(1)
if name == key:
k = match.group(2)
result.update({k:value})
return result
Now you can use it like this:
persons = get_post_dict(request.POST, 'person')
...
or
django.http.QueryDict.getdict = get_post_dict
persons = request.POST.getdict('person')
这篇关于Django和HTML数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!