2015-04-22 206 views
0

我正在嘗試在應用程序中設置會話,但似乎無法正常工作。登錄後,會話將顯示在Devtools上,但它不會重定向到/index,因爲它會這樣做。無法讓此會話正常工作

這裏是我做了什麼:

app.py

from flask import Flask, render_template, redirect, url_for, request, session, flash 
from functools import wraps 


app = Flask(__name__) 

app.secret_key = "gr3hu39ud3n89ud893e4" 


def login_required(f): 
    @wraps(f) 
    def wrap(*args, **kwargs): 
     if 'logged_in' in session: 
      return f(*args, **kwargs) 
     else: 
      flash('You need to login first') 
      return redirect(url_for('login')) 
    return wrap 


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


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


@app.route('/login', methods=('GET', 'POST')) 
def login(): 
    error = None 
    if request.method == 'POST': 
     if request.form['username'] != 'admin' or request.form['password'] != 'admin': 
      error = 'Invalid credentials. Please try again' 
     else: 
      session['logged_in'] = True 
      flash('You were just logged in!') 
      return redirect(url_for('home')) 
    return render_template('login.html', error=error) 

@app.route('/logout') 
@login_required 
def logout(): 
    session.pop('logged_in', None) 
    flash('You were just logged out!') 
    return redirect(url_for('welcome')) 

if __name__ == "__main__": 
    app.run(debug=True, host='0.0.0.0', port=5000) 

模板/ welcome.html

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Document</title> 
    <link rel="stylesheet" href="/static/css/bootstrap.css"> 
    <link rel="stylesheet" href="/static/css/bootstrap.css.map"> 
</head> 
<body> 
    <div class="container"> 
     <h1>Welcome to flask</h1> 
     <p>Click <a href="/">Here</a> to go home</p> 
     {% for message in get_flashed_messages() %} 
      {{ message }} 
     {% endfor %} 
    </div> 
</body> 
</html> 

模板/ index.html的

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Document</title> 
    <link rel="stylesheet" href="/static/css/bootstrap.css"> 
    <link rel="stylesheet" href="/static/css/bootstrap.css.map"> 
</head> 
<body> 
    <div class="container"> 
     <h1>Welcome to flask</h1> 
     <p>Click <a href="/logout">Here</a> to go Logout</p> 
     {% for message in get_flashed_messages() %} 
      {{ message }} 
     {% endfor %} 
    </div> 
</body> 
</html> 

templates/login.html

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Document</title> 
    <link rel="stylesheet" href="/static/css/bootstrap.css"> 
    <link rel="stylesheet" href="/static/css/bootstrap.css.map"> 
</head> 
<body> 
    <div class="container"> 
     <h1>Please Login</h1> 
     <br> 
     <form action=""> 
      <input type="text" placeholder="Username" name="username" value="{{ request.form.username }}"> 
      <input type="password" placeholder="Password" name="password" value="{{ request.form.password }}"> 
      <input type="submit" class="btn btn-default" value="Login"> 
     </form> 
     {% if error %} 
      <p class="error"><strong>Error</strong>{{error}}</p> 
     {% endif %} 
    </div> 
</body> 
</html> 

回答

1

登錄腳本中的<form>沒有method屬性。沒有method屬性,form默認使用GET

模板/ login.html的試試這個:

... 
<form method="POST" action=""> 
... 

參考:

+0

天啊!我真的很慚愧。非常感謝@羅布。 – Yelp