python - 循环遍历表单 POST 将数据存储在 python 变量中

标签 python django

我有一个包含多个输入的表单...在 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/

相关文章:

java - Python 与 Java 和 C 中的长类型

python - 根据请求动态选择数据库

html - 带有美化功能的 Vscode 不会缩进 django html 模板

python - Django 应用仅使用默认数据库来检索数据。如何将检索功能设置为mysql数据库

python - 如何根据模式将列表拆分为子集?

python - 有没有更快的方法将 2d numpy 数组保存到 png

python - 类型错误 : 'GroupedData' object is not iterable in pyspark

django - 停止 celeryd 的问题

python - 当我在 gunicorn 中为 Django 应用程序设置 worker 超时时,其他用户无法访问该应用程序

python - 使用 Django Rest Framework 作为文件系统处理的安全层