【问题标题】:Python calling function from other function and passing parameter [duplicate]Python从其他函数调用函数并传递参数[重复]
【发布时间】:2020-12-15 17:47:12
【问题描述】:

对于那些因为看到有相关答案而对我的问题投反对票的人。我看到的唯一相关问题/答案是关于重定向到完整 URL。我想知道如何重定向到另一个函数,以便我可以传递一个参数。请在下结论之前重新阅读我的问题。

我的问题:如果用户被定向到 /userLogoutGo,我希望该函数将您看到的消息传递给索引函数并让该函数打开其模板。请帮我看看我做错了什么?谢谢!

from flask import Flask, request, render_template, redirect, url_for, session

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
@app.route('/userLogin', methods=['GET', 'POST'])
def index(myParam=''):
    if myParam != '':
        t_message = myParam
    else:
        t_message = "Login or Register"
    return render_template("user_register.html",
                           t_message=t_message,
                           t_js="user_register.js",
                           t_title=t_message,
                           id_user=id_user)
                           
@app.route('/userLogoutGo', methods=['GET', 'POST'])
def userLogoutGo():
    global id_user
    id_user = 0
    a = index("Logged out. Now you can Log In or Register")

【问题讨论】:

  • 我想你要做的是使用flask的重定向功能。
  • 您可以放心地使用"Login or Register" 作为默认参数而不是'',因为str 的值是不可变的。
  • 无论我使用 Flask 还是烧瓶都看到错误:return Flask.redirect(Flask.url_for('/userLogin')) AttributeError: type object 'Flask' has no attribute 'redirect' ``` return flask.redirect(flask.url_for('/​​userLogin')) NameError: name 'flask' is not defined```

标签: python flask redirect


【解决方案1】:

我认为您想要做的是使用烧瓶中的重定向功能。然后用户将被重定向到 loginurl 并运行 index 函数。

【讨论】:

    猜你喜欢
    • 2021-11-12
    • 1970-01-01
    • 2016-01-19
    • 2013-12-16
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 2016-05-27
    相关资源
    最近更新 更多