如何在python中循环通过httprequest post变量 [英] how to loop through httprequest post variables in python

查看:121
本文介绍了如何在python中循环通过httprequest post变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何循环遍历Django中的HttpRequest post变量?

How can you loop through the HttpRequest post variables in Django?

我有

for k,v in request.POST:
     print k,v



<

which is not working properly.

谢谢!

推荐答案

request.POST 是一个类似字典的对象,包含所有给定的HTTP POST参数。

request.POST is a dictionary-like object containing all given HTTP POST parameters.

当您循环访问 request.POST 时,只能获取密钥。

When you loop through request.POST, you only get the keys.

for key in request.POST:
    print(key)
    value = request.POST[key]
    print(value)

要一起检索密钥和值,请使用 项目 方法。

To retrieve the keys and values together, use the items method.

for key, value in request.POST.items():
    print(key, value)

请注意, request.POST 可以包含每个键的多个项目。如果您期望每个键的多个项目,您可以使用 列表 ,它将所有值作为列表返回。

Note that request.POST can contain multiple items for each key. If you are expecting multiple items for each key, you can use lists, which returns all values as a list.

for key, values in request.POST.lists():
    print(key, values)

有关更多信息,请参阅Django文档 QueryDict

For more information see the Django docs for QueryDict.

这篇关于如何在python中循环通过httprequest post变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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