PHP的表单括号技巧是Django的___? [英] PHP's form bracket trick is to Django's ___?

查看:105
本文介绍了PHP的表单括号技巧是Django的___?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,您可以创建名称如下的表单元素:

In PHP you can create form elements with names like:

category[1]
category[2]

甚至

category[junk]
category[test]

发布,类别会自动变成一个不错的字典,如:

When the form is posted, category is automatically turned into a nice dictionary like:

category[1] => "the input value", category[2] => "the other input value"

有没有办法在Django中做到这一点? request.POST.getlist 不太正确,因为它只是返回一个列表,而不是字典。我也需要钥匙。

Is there a way to do that in Django? request.POST.getlist isn't quite right, because it simply returns a list, not a dictionary. I need the keys too.

推荐答案

你可以使用 django.utils.datastructures.DotExpandedDict 输入命名类别.1,category2等做类似的事情,但是我不明白为什么你会有必要验证并重新显示你收到的信息,当使用django.forms.Form将做一切您 - 适当的字段将为您调用 getlist 方法,前缀参数可用于重复使用相同的表单次。

You could use django.utils.datastructures.DotExpandedDict with inputs named category.1, category.2 etc. to do something similar, but I don't really see why you would if you ever have to validate and redisplay the information you're receiving, when using a django.forms.Form will do everything for you - appropriate fields will call the getlist method for you and the prefix argument can be used to reuse the same form multiple times.

这篇关于PHP的表单括号技巧是Django的___?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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