【发布时间】:2023-03-11 02:40:01
【问题描述】:
我在我的模板中使用 for 循环,并且我使用数组的每条记录的不同 id
{% 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 %}
现在,在我的视图中,我想将此数据保存到我的数据库中。但首先我检查了我的数组是否返回了某些东西。所以我只是尝试将其打印为。
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'title': [u''], u'customatt': [u'', u''], u'csrfmiddlewaretoken': [u'up4Ipd5L13Efk14MI3ia2FRYScMwMJLz']}>"
但是如果我打印这个
q = upload_form.data['custom[1]']
然后显示数组 1 的值。
所以请建议我一个更好的方法来做到这一点,我可以在views.py中显示我的数组中的所有值
【问题讨论】:
-
就像我在回复您的last question 时所说,使用 django 表单库。
-
是的,我正在看它........但是这里面临关于如何显示数组值的问题..因为我的数组接受了这些值,但我无法显示或显示使用它,所以如果你建议我以某种方式做到这一点,那么它对我来说要好得多。 感谢您向我推荐有关表单集的信息
标签: django arrays django-templates django-views