Flask设置和获取session案例

from flask import Flask, render_template, request,make_response,session,redirect,url_for
app = Flask(__name__)
app.secret_key = 'any random string'

@app.route('/')
def index():
    if 'username' in session:
        username = session['username']
        return ('Logged in as ' + username + '
' + "click here to log out") else: return ("You are not logged in
" + "click here to log in") @app.route('/login', methods = ['GET', 'POST']) def login(): if request.method == 'POST': session['username'] = request.form['username'] return redirect(url_for('index')) else: return render_template('new6.html') @app.route('/logout') def logout(): # remove the username from the session if it is there session.pop('username', None) return redirect(url_for('index')) if __name__ == '__main__': app.run(debug = True)

new6.html




    
    12212


你可能感兴趣的:(Python编程,python,学习)