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

标签 python django django-templates

我的模板中有这段代码:

{% 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/

相关文章:

django - 包含Django templatetag用户对象

c# - OpenCV - 创建颜色 mask

javascript - 从域中提取二级域? - Python

python - 为什么 Django 会为代理模型创建迁移文件?

python - 为什么在 Django models.Manager 中定义 create_foo() 而不是覆盖 create()?

javascript - 在javascript中访问python字典

python - 在 opengl/pycuda 中可视化 n 维标量场

python - 需要从一个txt文件读入字典

python - 从 Pandas Condition 获取行号和列号

javascript - 表单向导不会移至 jquery 对话框中的下一个屏幕