我有一个包含多个输入的表单...在 request.POST
中,我循环遍历所有输入值。但我想将它们存储在一个变量中。我该怎么做?
for key, value in request.POST.items():
print(key, value) # how can I store instead of print?
如何将所有值存储在 python 数组/字典/其他内容中?
最佳答案
您有多种方法可以将 POST 数据存储在局部变量中。问题是:当您有权访问 request.POST
时,为什么要这样做?
# Note: all keys and values in these structures are strings
# easiest, but immutable QueryDict
d = request.POST
# dict
d = dict(request.POST.items())
# array
a = list(request.POST.items()) # list of key-value tuples
a = request.POST.values() # list of values only
这些变量仅在当前请求-响应周期内有效。如果您想保留除此之外的任何数据,则必须将它们存储到数据库中。此外,我建议使用 django form处理 POST 数据。这将为您处理验证、类型转换等工作。
关于python - 循环遍历表单 POST 将数据存储在 python 变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37464256/