我目前在我的Flask Web应用程序上有一个具有+-100个输入的表单。要请求1行中的每个值,我使用:f = request.form
。如果将其打印在本地版本上,则最终将得到完整的所有值的完整列表:
ImmutableMultiDict([('intro1', 'hello'), ('intro2', 'eargr'), ('intro3', 'rferferaf'), ('intro4', 'eragaerg'), ('intro5', 'aergferaf'), ('intro6', 'aerfa'), ('intro7', 'faf'),`('intro8', 'fazfa'), ('intro9', 'f'), ('intro10', 'f'), ('intro11', 'f'), ('intro12', 'ezfzef'),` ('intro13', 'f'), ('liab1', ''), ('liab2', 'hello'),
('liab3', ''), ('liab4', 'nvion'),....
如果我将其打印在实时版本上,它将以完全随机的顺序返回:
ImmutableMultiDict([('liab43', 'hello'), ('pl33', ''), ('inv3', ''), ('fin5', ''), ('inv10', ''), `('pl44', ''), ('liab46', ''), ('liab17', ''), ('liab49', ''), ('intro10', 'ovov o'), ('fin9', ''), ('pl30', ''), ('pl15', ''), ('liab10', ''), ('pl34', 'hello'), ('pl24', ''), ('intro13', 'nvion'), ('liab31', ''), ('pl39', ''), ('intro3', 'zenfoczoi'), ('liab1', ''), ('inv15', ''), ('pl16', ''), ('liab50', ''), ('intro1', 'hello'), ('intro8', 'connfvo'), ('pl38', ''), ('fin4', ''), ('pl49', ''), ('pl21', ''), ('fin6', ''), ('intro2', 'hefhuze'), ('liab22', ''), ('pl61', ''), ('pl18', ''), ('fin10', ''), ('liab53', ''), ('liab30', ''),`
我该如何解决?因为两个版本完全相同,所以没有区别。 Windows上为本地版本,而使用nginx和gunicorn在Debian VPS上为实时版本。
答案 0 :(得分:0)
默认情况下,当您调用request.form
时,Flask返回ImmutableMultiDict
(不可变字典),但是如果您想要数据,也可以让它返回ImmutableOrderedMultiDict
(有序不可变字典)为了。只需这样做:
from flask import Flask, Request
from werkzeug.datastructures import ImmutableOrderedMultiDict
class MyRequest(Request):
"""Request subclass to override request parameter storage"""
parameter_storage_class = ImmutableOrderedMultiDict
class MyFlask(Flask):
"""Flask subclass using the custom request class"""
request_class = MyRequest
app = MyFlask(__name__)