Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法登录flask-始终询问用户名&;密码_Python_Flask - Fatal编程技术网

Python 无法登录flask-始终询问用户名&;密码

Python 无法登录flask-始终询问用户名&;密码,python,flask,Python,Flask,我看了一个教程(),看到了在flask中登录网页是多么容易。我试了好几次,但当我输入正确的用户名和密码时(保存框会弹出一段时间来记住我的数据),带有编辑文本的小框消失了,它会再次询问。我做错了什么 谢谢 from flask import Flask, render_template, request, make_response import os app = Flask(__name__) @app.route('/pass') def index(): if request.a

我看了一个教程(),看到了在flask中登录网页是多么容易。我试了好几次,但当我输入正确的用户名和密码时(保存框会弹出一段时间来记住我的数据),带有编辑文本的小框消失了,它会再次询问。我做错了什么

谢谢

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>

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

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

127.0.0.1 - - [13/Nov/2017 17:33:10] "GET /pass HTTP/1.1" 200
在检查授权之前和之后打印request.authorization以查看传递的内容:

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'})
第一次打印应为无,授权后第二次打印应为带有用户名和密码的词典:

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

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

除了使用<代码>授权< /代码>之外,您可以考虑表单输入。下面是一个创建简单登录系统的简单示例

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\u 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>

简单登录
{%with flash_messages=get_flash_messages()%}
{%if flash_messages%}
    {flash_messages%}中的消息为%
  • {{message}}
  • {%endfor%}
{%endif%} {%endwith%} 用户名:
密码:
输出:


要了解有关
flash
的更多信息,请查看中的示例,这很奇怪。我检查了你的代码,它正在工作。您的设置中还有其他内容吗?非常感谢您的快速回复!还有什么会影响这一点?您使用的是什么版本的flask、python?您使用的浏览器是什么?Python2.7.13-Flask'0.12.2'-Chrome和Firefox如何运行代码?你在使用virtualenv吗?如果是,你会激活它吗?对不起,我是新来的。在哪里可以检查打印语句的结果?调试日志在哪里?谢天谢地,它会在控制台窗口中打印出来,也就是启动服务器的窗口。我有一个谷歌云虚拟实例,并指定了这个虚拟主机。这有什么问题吗?如果使用apache运行flask,则日志文件位于虚拟主机(${apache_log_DIR}/access.log)中指定的位置。但是,既然您说您是用python init.py运行它,我怀疑您是用apache运行它的。我强烈建议您在您的计算机上设置本地开发环境,并在了解flask的工作原理后尝试Google部署。谢谢,但我得到了不允许的方法请求的URL不允许使用该方法。你知道为什么吗?我已经在我的本地机器上运行了代码,并且运行得很顺利。您是否在主机中正确配置了
flask
?你能发布完整的追溯吗?你是对的,我在本地试用过,效果很好。你说的完整追溯是什么意思?我需要检查flasked是否正确设置了哪些文件?非常感谢!我说的回溯是指错误日志。我没有使用谷歌云来托管Flask应用程序。因此,我在这方面无能为力。谷歌官方文档:您的解决方案在数字海洋液滴中工作,但在谷歌云虚拟机中,我仍然得到不允许的方法。现在我使用的是数字海洋水滴,谢谢你的帮助
<!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>