无法登录烧瓶 - 始终询问用户名和密码

时间:2017-11-13 15:20:16

标签: python flask

我看了一个教程(https://www.youtube.com/watch?v=VW8qJxy4XcQ),我看到登录到烧瓶网页是多么容易。我尝试了几次,但是当我输入正确的用户名和密码(保存框出现片刻以记住我的数据)时,带有编辑文本的小盒子消失了,它再次询问它们。我做错了什么?

由于

from flask import Flask, render_template, request, make_response
import os
app = Flask(__name__)


@app.route('/pass')
def index():
    if request.authorization and request.authorization.username == 'a' and request.authorization.password == 'a':
        return 'logged in'
        #return render_template("index2.html")
    else:
        return make_response('Could not verify!', 401, {'WWW-Authenticate' : 'Basic realm=Login required'})

if __name__ == "__main__":
    app.run(debug=True)

虚拟主机:

<VirtualHost *:80>
                ServerName 39.234.23.12
                ServerAdmin youemail@email.com
                WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
                <Directory /var/www/FlaskApp/FlaskApp/>
                        Order allow,deny
                        Allow from all
                </Directory>
                Alias /static /var/www/FlaskApp/FlaskApp/static
                <Directory /var/www/FlaskApp/FlaskApp/static/>
                        Order allow,deny
                        Allow from all
                </Directory>
                ErrorLog ${APACHE_LOG_DIR}/error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

无论是在本地进行,还是在谷歌云虚拟实例上都没有用;

import os
from flask import Flask, render_template, request

app = Flask(__name__)

APP_ROOT = os.path.dirname(os.path.abspath(__file__))

@app.route("/")
def index():
    if request.authorization and request.authorization.username == 'a' and request.authorization.password == 'a':
        return "success"
    return "nope"

2 个答案:

答案 0 :(得分:1)

除了使用authorization之外,您可以考虑表单输入。这是一个创建简单登录系统的简单示例。

app.py

from flask import Flask, request, render_template, flash

app = Flask(__name__)
app.secret_key = 'TOO_SECRET_TO_REVEAL'

@app.route('/',methods = ["GET", "POST"])
def index():
    if request.method == "POST":
        username = request.form.get("username", None)
        password = request.form.get("password", None)
        if username == "shovon" and password=="password":
            flash("Login successful")
        else:
            flash("Wrong username or password")
    return render_template("simple_login.html")

if __name__ == '__main__':
    app.run(debug = True)

然后在templates文件夹中simple_login.html包含:

<!DOCTYPE html>
<html>
<head>
    <title>Simple Login</title>
</head>
<body>
    {% with flash_messages = get_flashed_messages() %}
        {% if flash_messages %}
            <ul>
                {% for message in flash_messages %}
                    <li>
                        {{ message }}
                    </li>
                {% endfor %}
            </ul>
        {% endif %}
    {% endwith %}
    <form action="/" method="post">
        Username: <input type="text" name="username" /> <br>
        Password: <input type="password" name="password" /> <br>
        <input type="submit" name="login" value="Login" />
    </form>
</body>
</html>

<强>输出:

login_demo

要详细了解message flashing

中的flash观看示例

答案 1 :(得分:0)

代码应该工作,看起来更像浏览器的问题。尝试将浏览器设置重置为默认值。

问题仍然存在,首先检查控制台中的调试日志,并确保在登录尝试后响应状态代码为200.

def index():
    print(request.authorization)
    if request.authorization and request.authorization.username == 'a' and request.authorization.password == 'a':
        print(request.authorization)
        return 'logged in'
    else:
        return make_response('Could not verify!', 401, {'WWW-Authenticate' : 'Basic realm=Login required'})

在检查授权之前和之后打印request.authorization以查看传递的内容:

{'username': 'a', 'password': 'a'}

首次打印应该是None,授权后第二次打印应该是带有用户名和密码的字典:

document.getElementbyId

成功授权后,两者都应返回带有用户名和密码的字典。