Python Flask 用户登录重定向

标签 python redirect nginx flask gunicorn

我已经构建了一个非常简单的用户登录系统,没有数据库,但是重定向又是一个问题。如果 html 文件中提交的用户名和密码正确,则 python 会执行以下操作:

@app.route("/", methods=['GET', 'POST'])
def login_page():
    if request.method == 'POST':
        attempted_username = request.form['username']
        attempted_password = request.form['password']

        if attempted_username == 'admin' and attempted_password == 'password':
            return redirect(url_for('index'))
        else:
            error='E-Mail or Password not available'
    return render_template('login.html', error=error)

现在 URL 变为以下网址:shost/index 然后 Chrome 就会告诉我

ERR_NAME_NOT_RESOLVED
The DNS address of the shost server couldnt be found.

为什么 URL 没有变成 server_IP/index,例如127.0.0.1/index,因为这个可以在我的浏览器中运行。如何防止 Flask 发出 shost 问题?

这也是登录的 html 表单代码:

<form class="text-left" method="post" action="">
    <input class="mb0" type="text" placeholder="Username" name="username" value="{{request.form.username}}"/>
    <input class="mb0" type="password" placeholder="Password" name="password" value="{{request.form.password}}"/>
    <input type="submit" value="Login"/> 
</form>

代码的 @app.route("/index") 部分如下所示:

@app.route("/index")
def index():
    return render_template('index.html')

非常感谢和最诚挚的问候

最佳答案

看起来您没有渲染登录页面,您只是告诉 python 如果使用 POST 则生成索引页面,但尚未使用 POST,因为尚未完成任何表单。此外,在返回重定向(url_for('index'))中,您需要添加“app.” 。

尝试这样的事情。

@app.route('/', methods=['GET', 'POST']) 
def login():
    if request.method == 'POST':
        attempted_username = request.form['username']
        attempted_password = request.form['password']

        if attempted_username == 'admin' and attempted_password == 'password':
            return redirect(url_for('app.index'))

    return render_template('loginpage.html') 

关于Python Flask 用户登录重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45890664/

相关文章:

python - l[ :] performance problem

Wordpress 重定向到子目录 - wp-admin 转发到错误的登录 URL

Magento Controller url 重定向到仪表板

zend-framework - 在前端 Controller 插件 Zend 中重定向

node.js - 在 nginx 上托管多个 Node.js 实例

php - 拒绝访问 PHP 文件 (Nginx)

python - 如何计算Tensorflow中训练RNN语言模型的准确率?

python - 如何运行 Python 程序?

python - 存储 8M+ sha256 哈希的最有效内存方式

nginx - 在 nginx 提供的 HTML 文件中包含主机名