一:flask认知
flask 短小精悍,可扩展强
flask 所有扩展的网址,flask.pocoo.org/externsions
有别于其他的web框架的地方:flask做了一个上下文管理机制
二:
flask依赖WSGI
WSGI:是web服务网关接口
本质:创建socket服务端,接收请求
基于werkzeug
三:最简单的flask程序
from flask import Flask app = Flask(__name__) @app.route("/index") def index(): return "hello word!" app.run()
四:返回页面
from flask import Flask,render_template app = Flask(__name__) @app.route("/login") def login(): return render_template("login.html") app.run()
五:一个简单用户登录验证程序
from flask import Flask,render_template,request,redirect,session # app = Flask(__name__,template_folder="templates",static_folder="staticccc",static_url_path='/vvvvv') app = Flask(__name__,template_folder="templates",static_folder="static") app.secret_key = 'abcdefg'
@app.route('/login',methods=["GET","POST"]) def login(): if request.method == 'GET': return render_template('login.html') user = request.form.get('user') pwd = request.form.get('pwd') if user == 'www' and pwd == '123': session['user'] = user return redirect('/index') return render_template('login.html',error='用户名或密码错误') # return render_template('login.html',**{"error":'用户名或密码错误'}) @app.route('/index') def index(): user = session.get('user') if not user: return redirect('/login') return render_template('index.html') if __name__ == '__main__': app.run()