1:会话:
from flask import Flask, url_for, request, render_template, session from werkzeug.utils import redirect, escape app = Flask(__name__) @app.route(‘/‘) def index(): if ‘username‘ in session: return ‘Logged in as %s‘ % escape(session[‘username‘]) return ‘You are not logged in‘ @app.route(‘/login‘, methods=[‘GET‘, ‘POST‘]) def login(): if request.method == ‘POST‘: session[‘username‘] = request.form[‘username‘] return redirect(url_for(‘index‘)) return ‘‘‘ <form action="" method="post"> <p><input type=text name=username> <p><input type=submit value=Login> </form> ‘‘‘ @app.route(‘/logout‘) def logout(): # remove the username from the session if it‘s there session.pop(‘username‘, None) return redirect(url_for(‘index‘)) if __name__ == ‘__main__‘: app.debug=True app.run()
2: 部署到 Web 服务器
准备好部署你的 Flask 应用了?你可以立即部署到托管平台来圆满完成快速入门,以下厂商均向小项目提供免费的方案:
托管 Flask 应用的其它选择:
如果你有自己的主机,并且准备自己托管,参见 部署选择 章节。