如何在python中遍历httprequest post变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3303336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:21:44  来源:igfitidea点击:

how to loop through httprequest post variables in python

pythondjangoposthttprequest

提问by icn

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

如何在 Django 中遍历 HttpRequest post 变量?

I have

我有

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

which is not working properly.

这不能正常工作。

Thanks!

谢谢!

采纳答案by Alasdair

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

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

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

当你循环时request.POST,你只能得到钥匙。

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

To retrieve the keys and values together, use the itemsmethod.

要一起检索键和值,请使用items方法。

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

Note that request.POSTcan 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.

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

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

For more information see the Django docs for QueryDict.

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