如何使用 Flask Request 将 GET 参数传递给 url

How to pass GET parameters to url using Flask Request

我知道如何使用 Flask 请求 获取 来自 url 的请求参数:request.args.get('<param>')。事实上,这是我在搜索标题问题时唯一能找到的东西:

我需要知道如何发送用户url的请求参数。

例如,flask_login 有一个名为 "next" 的参数,它存储用户在访问包装在 @login_required 中的路由时访问的 url,以便他们可以在他们登录后重定向到它。我需要用我自己的表单实现这个确切的功能,但我找不到实现它的方法。

flask-login代码的相关部分是here,但我不明白他们是如何传递参数的。

我需要一种方法来记录用户的 url,当他们单击 link 到表单页面时,将其作为 GET 请求参数传递到他们在表单页面上的 url ,并在他们提交或取消表单时重新访问它 return 他们到他们的前一页。

Per the documentation 我找到了答案(@E.Serra 为正确方向的动力):

flask.url_for(endpoint, **values)

Generates a URL to the given endpoint with the method provided.

Variable arguments that are unknown to the target endpoint are appended to the generated URL as query arguments. If the value of a query argument is None, the whole pair is skipped. In case blueprints are active you can shortcut references to the same blueprint by prefixing the local endpoint with a dot (.).

所以如果你有路线

@app.route('/view/<variable>/')
def view(variable):
    pass

来电

url_for('view', variable='parameter', variable2='parameter2')

将生成一个 url,其中 parameter2 是一个查询参数。