gpt4 book ai didi

python - 获取 Django View 中的复选框列表

转载 作者:IT老高 更新时间:2023-10-28 20:39:03 27 4
gpt4 key购买 nike

我的模板中有这段代码:

{% for email in emails %}
{%if email%}
<input type="checkbox" name="email" value="{{email}}" /> {{email}}<br />
{% endif %}
{% endfor %}

所以模板中的输出是:

email1@sample.com
email2@sample.com
email3@sample.com
...

在我看来,我为此打印了我的 request.POST。

print request.POST
print request.POST['email']
for email in request.POST['email']:
print email

我得到了这个输出:

<QueryDict: {u'email': [u'email1@sample.com', u'email2@sample.com', u'email3@sample.com'], ..., u'csrfmiddlewaretoken': [u'05e5bdb542c3be7515b87e8160c347a0', u'05e5bdb542c3be7515b87e8160c347a0']}>
email3@sample.com
e
m
a
i
l
3
@
s
a
m
p
l
e
.
c
o
m

我的问题是,如何在我的 View 中获取电子邮件列表?我希望输出是这样的..

email1@sample.com
email2@sample.com
email3@sample.com
...

最佳答案

使用 request.POST.getlist :

Returns the data with the requested key, as a Python list. Returns an empty list if the key doesn’t exist and no default value was provided. It’s guaranteed to return a list of some sort unless the default value provided is not a list.

关于python - 获取 Django View 中的复选框列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10276753/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com