初期学习Flask时,可能会用到下面的内容来实现某种功能,这里简单记录下,后期可再来回顾。
一、密码机制
用户在登录时,密码不加密的话容易破解查看,所以需要用到加密
1、密码加密:
from werkzeug.security import generate_password_hash, check_password_hash
user.password = generate_password_hash(password) # ---> 加密 ,用于注册使用
# sha256加密,长度为64位,所以数据库密码长度限制必须大于64位
2、密码检查:
check_password_hash(user.password, password) # --->返回:bool 用于登录时检查密码是否一致
# user.password --> 数据库中的密码
# password --> 用户输入的密码
二、会话机制 cookie & session:
http是无状态协议,所以需要记录用户的信息或者状态,来支持后续的操作,也方便我们的逻辑取值或者校验
1、保存cookie,通过response对象保存
response = redirect(xx)
response = render_template(xx)
response = Response()
response = make_response()
response = jsonify(xx)
# 通过对象调用方法
response.set_cookie(key, value, max_age= xxx)
# --value必须是str
2、获取cookie,通过request对象获取
request.args.get()
request.form.get()
# cookie也在request对象中
request.cookie.get(key) # ---->> 取key值
3、删除cookie,通过response对象删除
response = redirect(xx)
response = render_template(xx)
response = Response()
response = make_response()
response = jsonify(xx)
# 通过对象调用方法
response.delete_cookie(key)
session:
1、保存session
from flask import session
user= User.query.get('uid')
session['uid'] = user.id
2、获取session
uid = session.get('uid', None)
3、删除session
del session['uid']
cookie是存放在客户端,session是存放在服务器
三、钩子函数
先说下钩子函数的使用例子:比如一个登录权限的验证,只要访问用户中心,就需要判断用户是否是登录状态,如果用户登录了,可以正常显示页面,如果没有,则自动跳转到登录页面进行登录,登录之后才可以进行查看,所以我们可以每次执行视图之前先执行钩子函数(before_request),由它做判断当前用户是否登录
1、直接应用在app上
from flask import Flask, request
app = Flask(__name__)
@app.before_first_request # -->>1、请求接口前执行,但是只会执行一次
def my_before_first_request():
print('开始只运行一次,before_app_first_request')
@app.before_request # -->>2、请求接口前,每次都会执行一次,在before_app_first_request之后执行
def my_before_request():
print('每次都会运行,before_app_request ,request对象请求的路径:%s' % request.path)
@app.after_request # -->3、视图函数执行之后才执行
def my_after_request():
print('after_app_request, 在视图函数执行之后,再执行这个')
@app.teardown_request # -->> 4、在after_app_request 钩子函数执行之后才执行
def my_teardown_request():
print('teardown_app_request, 在after_app_request执行之后,再执行这个')
@app.route('/')
def my():
print('我是视图函数')
2、应用于蓝图上(多了个app)
# __init__.py
from flask import Flask, request, user_bp
app = Flask(__name__)
app.register_blueprint(user_bp)
# view.py
user_bp = Blueprint('user', __name__)
@user_bp.before_first_request # -->>1、请求接口前执行,但是只会执行一次
def my_before_first_request():
print('开始只运行一次,before_app_first_request')
before_app_request(第二步执行)
after_app_request (第三步执行,但是需要先执行视图函数,才能执行这个)
teardown_app_request(第四步执行)
# 使用方式都是和上面应用在app上的方式一样,这里不再重复编写
针对上面的登录权限这里做个例子:
# 重点
@user_bp.before_app_request # -->>请求接口前,每次都会执行一次,在before_app_first_request之后执行
def my_before_request():
route_list = ['/user/center', ......]
if request.path in route_list: # 判断此次访问的路径在不在这个route_list列表中
uid = session.get('uid') # 这个需要登录后在session中设置uid,否则这样取不到值
if not uid:
return '需要跳转到登录页' # redirect(url_for('user.login'))
else:
return '当前是登录状态'
else:
pass # 不需要登录即可访问
四、paginate:分页函数
当有多条记录在页面显示时,不可能都显示在一页,所以我们用到了分页,来保证美观以及便于阅读。
paginate() 方法的返回值是一个 Pagination 类对象,这个类在 Flask-SQLAlchemy 中定义。
这个对象包含很多属性,用于在模板中生成分页链接,因此可以将其作为参数传入了模板。
pagination = Article.query.order_by(-Article.create_at).paginate(
page=1,
per_page=3
)
# -Article.create_at: 降序
# paginate(page=1, per_page=3)
# 解释:从第一页开始,每一页内容为3个,内容数量变化,总页数就会变化
print(pagination.items)
# 文章列表对象 [<Article 4>, <Article 3>, <Article 2>]
1、获取当前的页码数
print(pagination.page) # 比如 第3页
2、获取当前页的前一页的页码数
print(pagination.prev_num) # 第2页
3、获取当前页的后一页的页码数
print(pagination.next_num) # 第2页
4、判断是否有上一页或者下一页
print(pagination.has_next) # True
print(pagination.has_prev) # True
5、总共几页(根据分页量计算)
print(pagination.pages) # 可以分为10页
6、总条数
print(pagination.total)
在html文件中的应用
<!--------分页功能开始------->
<nav aria-label="Page navigation" class="col-md-offset-5">
<ul class="pagination">
<!--------点击上一页按钮开始------->
<li {% if not pagination.has_prev %} class="disabled" {% endif %}>
<!-------判断上一页是否可以点击------->
{% if pagination.has_prev %}
<a href="{{ url_for('user.home') }}?page={{ pagination.prev_num }}"
aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
{% else %}
<span aria-hidden="true">«</span>
{% endif %}
</li>
<!-------点击上一页按钮结束------>
<!-------根据文章动态创建页数开始-------->
{% for page_num in range(1, pagination.pages + 1) %}
<!----判断选择的页数并激活显示----->
<li {% if pagination.page == page_num %}
class="active"
{% endif %} >
<a href="{{ url_for('user.home') }}?page={{ page_num }}">
{{ page_num }}
</a>
</li>
{% endfor %}
<!-------根据文章动态创建页数结束------->
<!-------点击下一页按钮开始----------->
<li {% if not pagination.has_next %} class="disabled" {% endif %}>
<!-----判断下一页是否可以点击------>
{% if pagination.has_next %}
<a href="{{ url_for('user.home') }}?page={{ pagination.next_num }}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
{% else %}
<span aria-hidden="true">»</span>
{% endif %}
</li>
<!---------点击下一页按钮结束--------->
</ul>
</nav>
<!--------分页功能结束------->
上面的html可以定义一个宏的形式实现的分页导航,然后再其他地方引用,这样不用每次都写了(上面需要略作修改)
五、获取上传文件内容
html文件内容
<!--上传文件的内容:-->
<input type="file" name="photo">
视图函数中获取模版中上传的照片
# view.py
photo = request.files.get('photo')
# photo类型: FileStorage
filename = photo.filename # 获取名称,例如:test.jpg
name, suffix = filename .rsplit('.')
# 获取图片名和后缀 ->>['test','jpg']
file_path=os.path.join('xx/xx/', filename)
# 注意点:路径最后一定要加斜杠,防止粘连:xx/xx/
photo.save(file_path)
# 将图片保存到文件的指定路径
#也可以存入到数据库的字段中,假设当前有一个User模型
user = User.query.get(1)
user.photo = file_path # 将user表的字段值设置为file_path
六、Redis应用(缓存键值对及视图函数):
使用背景:
例如有时我们访问网站某个页面时因为图片内容过多,或者文件内容过大等各种原因,导致每次加载时都过慢,这时我们可以把内容缓存起来,下次直接使用缓存,在有效时间内可以快速访问,无需再次加载
使用前下载第三方库:
pip install redis
pip install flask-caching
1、在项目中应用的位置:
# ext
# |---__init__.py
from flask-caching import Cache
cache = Cache()
# apps
# |----__init__.py
from ext import cache
from flask import Flask
config={
'CACHE_TYPE': 'redis',
'CACHE_REDIS_HOST': '127.0.0.1',
'CACHE_REDIS_PORT': 6379
}
def create_app():
app = Flask(__name__)
......
cache.init_app(app=app, config=config)
return app
2、使用:
1、缓存键值对
# 设置:
cache.set(key, value, timeout=second)
cache.set_many([(key,value),(key1, value1),......]) # 设置多个
# 获取:
cache.get(key)
cache.get_many(key,key1,key2,......) # 获取多个
# 删除:
cache.delete(key)
cache.delete_many(key,key1,key2,......) # 删除多个
cache.clear() # 清空
2、视图函数的缓存
from extra import cache
user = Blueprint('user', __name__)
@user.route('/xx')
@cache.cached(timeout=50) # timeout 设置过期时间
def home():
......
pass
1908

被折叠的 条评论
为什么被折叠?



