如何制作 Flask request.form return 有序的字典数据?

How to make Flask request.form return ordered dict data?

我目前在我的 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'),....

如果我在我的实时版本上打印它,它会 return 完全随机顺序:

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 上。

默认情况下,当你调用request.form时,Flask return一个ImmutableMultiDict(不可变字典),但你也可以让它returnImmutableOrderedMultiDict (ordered immutable dict) 如果你想要数据有序。只需这样做:

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__)