Python Flask用户登录重定向

时间:2017-08-25 23:46:04

标签: 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)

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

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

为什么网址不是server_IP/index,例如127.0.0.1/index,因为这个可以在我的浏览器中使用。如何防止烧瓶发出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')

非常感谢和最好的问候

1 个答案:

答案 0 :(得分:1)

看起来你没有渲染登录页面,你只是告诉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')