标签:forms 请求 分离 函数 soc 网站 return request use
flask,是一个轻量级的框架,内置了:路由/视图/模板(jinja2)/cookie/session/中间件. 可扩展强,第三方组件非常多,例如:wtforms/flask-session/flask-sqlalchemy.
django,是一个重武器,django内置了很多功能方便我们使用,例如:admin/orm/模板/form/modelform/session/cookie/中间件/路由/缓存/信号/数据库的读写分离/分页...
flask,短小精悍可扩展强.
django,大而全重武器.
django好还是flask好?
小程序,flask比较好.
中大型,django比较好.
pip3 install flask
werkzurg是一个wsgi,本质上提供了socket服务端,用于接收用户请求.
django和flask一样,它们内部都没有实现socket服务端,需要依赖wsgi.
from wsgiref.simple_server import make_server
class WSGIHandler(object):
def __call__(self,environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return [bytes('<h1>Hello, web!</h1>', encoding='utf-8'), ]
if __name__ == '__main__':
obj = WSGIHandler()
httpd = make_server('127.0.0.1', 8000, obj)
httpd.serve_forever()
from werkzeug.wrappers import Response
from werkzeug.serving import run_simple
class Flask(object):
def __call__(self,environ, start_response):
return Response('Hello World!')(environ, start_response)
if __name__ == '__main__':
app = Flask()
run_simple('127.0.0.1', 5000, app)
from flask import Flask
# 实例化了一个Flask对象
app = Flask(__name__)
# 添加路由关系
@app.route('/index')
def index():
return '你好'
if __name__ == '__main__':
# 启动服务端
app.run()
关于返回值
from flask import Flask,render_template,request,redirect
app = Flask(__name__)
@app.route('/login',methods=['GET','POST'])
def login():
return "xxx"
return render_template('模板文件',参数)
return redirect(...)
return jsonify({"k1":123})
if __name__ == '__main__':
app.run()
关于模板
默认放在根目录的tempaltes
关于用户请求
request.method
request.form # POST
request.args # GET
session
加密的形式放在用户浏览器的cookie中.
from flask import Flask,render_template,request,redirect,jsonify,url_for,session
app.secret_key = "29ualknd872iuknsdgf"
def auth(func):
@functools.wraps(func)
def inner(*args,**kwargs):
user = session.get('user_info')
if not user:
return redirect(url_for('login'))
return func(*args,**kwargs)
return inner
@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 == 'alex' and pwd == 'dsb':
# 在session中存储一个值
session['user_info'] = user
return redirect(url_for('index'))
return render_template('login.html',error="用户名或密码错误")
在flask视图中添加装饰器
位置route的下面
记得加functools.wraps(...) , 保留函数的元信息.
from flask import Flask,render_template,request,redirect,jsonify,url_for,session
app.secret_key = "29ualknd872iuknsdgf"
def auth(func):
@functools.wraps(func)
def inner(*args,**kwargs):
user = session.get('user_info')
if not user:
return redirect(url_for('login'))
return func(*args,**kwargs)
return inner
@app.route('/index')
@auth
def index():
user_list = ["庄伯","亮哥",'实验']
user_info_list = [
{"name":'庄伯','age':18},
{"name":'亮哥','age':15},
{"name":'实验','age':18},
]
return render_template('index.html',user_list=user_list,user_info_list=user_info_list)
特殊装饰器:before和after request装饰器
from flask import Flask,render_template,request,redirect,jsonify,url_for,session
import functools
app = Flask(__name__)
app.secret_key = "29ualknd872iuknsdgf"
@app.before_request
def f1():
print('f1')
@app.before_request
def f11():
print('f11')
@app.after_request
def f2(response):
print('f2')
return response
@app.after_request
def f22(response):
print('f22')
return response
@app.route('/login')
def login():
print('login')
return 'Login'
@app.route('/index')
def index():
print('index')
return 'index'
if __name__ == '__main__':
app.run()
'''
f1
f11
login
f22
f2
'''
标签:forms 请求 分离 函数 soc 网站 return request use
原文地址:https://www.cnblogs.com/pythonblogs/p/12025020.html