Flask-进阶
Flask插件
flask-caching
安装
pip install flask-caching
初始化
from flask_cache import Cache
cache = Cache(config={
'CACHE_TYPE': 'simple',
})
cache.init_app(app=app)
使用
在视图函数上添加缓存
@blue.route('/')
@cache.cached(timeout=30)
def home():
print('加载数据')
return 'home'
钩子
常用的钩子函数
before_first_request: 处理第一次请求之前执行。
before_request: 在每次请求之前执行,通常使用这个钩子函数预处理一些变量,实现反爬等。
after_request: 注册一个函数,如果没有未处理的异常抛出,在每次请求之后运行。
teardown_appcontext: 当App上下文被移除之后执行的函数,可以进行数据库的提交或者回滚。
APP反爬策略
# 利用缓存反爬,相同ip地址1秒内不允许重复访问
key = request.remote_addr + "before"
value = cache.get(key)
if value:
return '123456'
else:
cache.set(key, 'aa', timeout=1)
# 反爬,防止非浏览器访问
ua = request.user_agent # 用户代理
if not ua:
return 'hello'
# abort(400) # 可以抛出错误给用户
Flask内置对象
g:
global全局对象
g对象是专门用来保存用户的数据的
g对象在一次请求中的所有的代码的地方,都是可以使用的
突破变量存储位置限制,为数据传递添加了新的方式,比如我们在before_request产生一个数据在后面
需要使用,可以保存在g对象中,在其他视图函数中就可以使用这个数据。
request:
请求对象,可以获取客户端提交过来的所有请求信息
session:
会话技术,服务端会话技术的接口
current_app:
app的配置信息,app对象获取,current_app
使用获取当前app需要注意,一定要在程序初始化完成之后
配置templates和static
如果想要修改templates模板目录或static静态目录,可以自己配置
在settings.py文件中添加BASE_DIR:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
在__init__.py文件中添加static路径和templates路径:
static_path = os.path.join(settings.BASE_DIR, 'static')
template_path = os.path.join(settings.BASE_DIR, 'templates')
app = Flask(__name__, static_folder=static_path, template_folder=template_path)
在views.py文件中访问模版:
@blue.route('/hello/')
def hello():
return render_template('hello.html')
在模板中使用静态资源:
<link rel="stylesheet" href="{{ url_for('static', filename='css/hello.css') }}">
前后端分离
# 前后端不分离:
# render_template('index.html', users=users)
# 前后端分离:
# 后端返回json字符串:jsonify()
# 前端使用ajax来请求数据: ajax
# jsonify(): 序列化
# HTTP请求方式:
# GET: 获取数据
# POST: 新增数据
# PUT: 修改数据
# DELETE: 删除数据
@blue.route('/users/', methods=['GET', 'POST', 'PUT', 'DELETE'])
def users():
if request.method == 'GET':
return jsonify({'method':'GET'})
elif request.method == 'POST':
return jsonify({'method':'POST'})
elif request.method == 'PUT':
return jsonify({'method':'PUT'})
elif request.method == 'DELETE':
return jsonify({'method':'DELETE'})
合集:
Python全栈(Flask)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库