python - Flask 重定向到 url 并传递查询字符串

标签 python flask

我有一个这样定义的 View :

@views.route('/issues')
def show_view():
    action = request.args.get('action')
method = getattr(process_routes, action)
return method(request.args)

在我的 process_routes 模块中,我想调用这个方法,并传递查询字符串值。我有以下代码:

return redirect(url_for('views.show_view', action='create_or_update_success'))

我在 process_routes 中有一个名为 create_or_update_success 的函数

我得到了

BuildError: ('views.show_view', {'action': 'create_or_update_success'}, None)

View 是一个蓝图。我可以成功调用

/issues?action=create_or_update_success

在我的浏览器中。

我做错了什么?

最佳答案

第一部分,views.,必须准确反射(reflect)您为 Blueprint() 对象提供的第一个参数。

不要试图将第一个参数设置为 __name__,因为在包内时它很可能包含模块的完整路径。在你的情况下,我怀疑是 some_package.views 而不仅仅是 views

Blueprint() 第一个参数改用字符串文字:

views_blueprint = Blueprint('views', __name__)

因此您可以引用 url_for('views.show_view') 而不会出现构建错误。

关于python - Flask 重定向到 url 并传递查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32058111/

相关文章:

python - 实际测试我构建的应用程序(Flask,Python)

python - 从 Sanic 应用程序的蓝图中检索配置

python - 在 flask test_request_context 中设置 remote_addr

python - 我们可以重载类对象的行为吗

python - 参数值正确性

python - 如何读取包含列表的文本文件并将这些列表添加到列表中?

python:在没有忙循环的情况下在不同的精确时间运行任务

android - 401 Unauthorized error.将授权码升级为凭证对象失败

python - 使用 Flask 为使用 create-react-app 创建的前端提供服务

python - Pyspark Dataframe 选择少数列上带有别名的所有列