Django和HTML阵列 [英] Django and HTML arrays

查看:157
本文介绍了Django和HTML阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格与此输入:

I have a form with this inputs:

<input name="person[name]" value="">
<input name="person[surname]" value="">
<input name="person[age]" value="">

当我提出,我怎么能分配一个HTML数组变量,导致request.POST.getlist(人)不工作,我的其他职位,但唯一一个我发现,没有被检查任何有用

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

<一个href=\"http://stackoverflow.com/questions/801354/django-equivalent-of-phps-form-value-array-associative-array\">THEAD

我希望有人能帮我指点迷津,造成读取文档,并没有完全得到做到这一点的方式...

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 %}

和我的我没有任何方法来赶上HTML阵列我送......有什么建议?

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

django.http.QueryDict.getdict = get_post_dict
persons = request.POST.getdict('person')

这篇关于Django和HTML阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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