【问题标题】:How to change to message when page is refreshed in flask?在烧瓶中刷新页面时如何更改为消息?
【发布时间】:2022-02-02 03:47:22
【问题描述】:

app.py 是:在此我使用 yash 和 yash 作为默认用户名和密码,当我输入错误密码并输入时,html 页面会打印“Invalid Credentials”。但是,当我在获得无效凭据后刷新时,它也会显示相同的 index.html 页面,其中包含“无效凭据”消息。从浏览器刷新页面时如何显示空白登录页面?

import flask
app = flask.Flask(name)
u_p={'yash':'yash'}
@app.route('/user/<name>')
def hello_user(name):
    return flask.render_template('hello.html',uname=name)
@app.route('/', methods=['GET', 'POST'])
def index():
    message = ''
    if flask.request.method == 'POST':
        username=flask.request.form['name-input']
        passowrd=flask.request.form['name-password']
        if u_p.get(username,'')==passowrd:
            return flask.redirect(flask.url_for('hello_user',name=username))
        else:
            message='Invalid Credentials'
    return flask.render_template('index.html', message=message)
if name == 'main':
    app.run()

我的index.html 是:

<!DOCTYPE html>

<html>
    <head>
        <title>Simple Flask App</title>
        <link rel="shortcut icon" href="{{url_for('static', filename='favicon.png')}}" type="image/x-icon">
    </head>
    <body>
        <h1>Login</h1>
        <form method="POST">
            username <input type="text" name="name-input"><br>
            password <input type="password" name="name-password"><br>
            <button type="submit">Submit</button>
        </form>
        <h2>New User : </h2>
        <button type="submit">Register</button>
        <p>{{message}}</p>
    </body>
</html>

【问题讨论】:

    标签: python flask login-page


    【解决方案1】:

    问题在于,如果您刷新页面,浏览器会再次发送相同的 POST 请求,因此就像您再次尝试使用相同的错误凭据登录一样。

    如果凭据错误,您可以通过重定向到索引来解决此问题 - 但是您不能将消息作为模板参数传递。

    幸运的是,flask 正是为此目的提供了消息闪烁:https://flask.palletsprojects.com/en/2.0.x/patterns/flashing/

    所以你的代码可能看起来像这样:

    import flask
    name = "main"
    app = flask.Flask(name)
    app.secret_key = "SECRET!"
    u_p={'yash':'yash'}
    @app.route('/user/<name>')
    def hello_user(name):
        return flask.render_template('hello.html',uname=name)
    @app.route('/', methods=['GET', 'POST'])
    def index():
        if flask.request.method == 'POST':
            username=flask.request.form['name-input']
            passowrd=flask.request.form['name-password']
            if u_p.get(username,'')==passowrd:
                return flask.redirect(flask.url_for('hello_user',name=username))
            else:
                flask.flash("Invalid Credentials")
                return flask.redirect(flask.url_for("index"))
        return flask.render_template('index.html')
    if name == 'main':
        app.run()
    

    index.html

    <!DOCTYPE html>
    
    <html>
        <head>
            <title>Simple Flask App</title>
            <link rel="shortcut icon" href="{{url_for('static', filename='favicon.png')}}" type="image/x-icon">
        </head>
        <body>
            <h1>Login</h1>
            <form method="POST">
                username <input type="text" name="name-input"><br>
                password <input type="password" name="name-password"><br>
                <button type="submit">Submit</button>
            </form>
            <h2>New User : </h2>
            <button type="submit">Register</button>
            {% with messages = get_flashed_messages() %}
                {% for message in messages %}
                    <p>{{message}}</p>
                {% endfor %}
            {% endwith %}
        </body>
    </html>
    

    附带说明:检查用户名和密码的方式是,有人可以使用随机用户名和空密码登录。

    【讨论】:

    • 你能告诉我这个程序这里需要什么秘钥吗
    • @YashwanthSai flash 函数需要Flask Session 来跟踪何时显示这些 flash 消息。为了让 Flask Session 工作,应用程序需要一个 secret_key,你应该将它设置为一个秘密的、安全的值。有关 secret_key 的更多信息,另请参阅 this question on StackOverflow
    猜你喜欢
    • 2013-05-26
    • 1970-01-01
    • 2018-01-21
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多