2017-08-25 122 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