gpt4 book ai didi

python - Flask 在 url_for 中传递未知的 Url 参数并重定向

转载 作者:太空宇宙 更新时间:2023-11-03 10:58:41 24 4
gpt4 key购买 nike

我很好奇是否有人想出了一个很好的解决方案来在 Flask 中传递未知的 URL 参数。我认为必须有一个简单的解决方案,我只是完全空白。感谢您的帮助。

假设我有 www.example.com?foo=baz&bar=buzz

@app.route('/')
def index():
foo = request.args.get('foo',None)
bar = request.args.get('bar',None)
return redirect(url_for('args', foo=foo, bar=bar, _external=True))

我似乎无法弄清楚的是传递任何未知参数的好方法。如果我有 www.example.com?foo=baz&bar=buzz&bing=bangwww.example.com?this=that 来到同一条路线,我需要得到 any/and 传递给下一个 View 的所有参数。

最佳答案

request.argsMultiDict .

A MultiDict is a dictionary subclass customized to deal with multiple values for the same key which is for example used by the parsing functions in the wrappers. This is necessary because some HTML form elements pass multiple values for the same key.

MultiDict implements all standard dictionary methods. Internally, it saves all values for a key as a list, but the standard dict access methods will only return the first value for a key.

假设您不介意多值问题,您可以将其用作任何字典并使用双 * 符号将其作为关键字参数传递:

url_for('test', _external=True, **request.args)

关于python - Flask 在 url_for 中传递未知的 Url 参数并重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36846103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com