2017-08-25 193 views
0

我已经构建了一个非常简单的用户登录系统,没有数据库,但重定向又是一个问题。如果从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. 

为什么ISN这个URL变成了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') 

非常感谢和问候

+0

请包括'login_page'和'index'路线的其余部分。 –

+0

@ edgaromar90感谢您的反馈。我添加了表单代码。 – saitam

+0

我想我知道你的问题是什么。让我看看你的代码的'@ app.route(“/ index”)部分。 –

回答

0

一点也没有看起来你正在渲染登录页面,如果使用POST,你只能告诉python生成索引页面,但是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') 
+0

编辑帖子。我使用redering – saitam