Django and HTML arrays

前端 未结 3 926
难免孤独
难免孤独 2021-01-15 12:05

I have a form with this inputs:





        
3条回答
  •  不要未来只要你来
    2021-01-15 12:30

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

提交回复
热议问题