![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Flask
Shao0000
这个作者很懒,什么都没留下…
展开
-
Flask---Models
Models: 在 models.py中: 创建模型: from flask_sqlalchemy import SQLAlchemy #创建操作数据库的对象 db = SQLAlchemy() class Person(db.Model): id = db.Column( db.Integer, primary_key=True, aut...原创 2018-12-01 12:25:44 · 590 阅读 · 0 评论 -
Flask---新建Flask:
新建Flask: 常用的: 在app.py中 #导入Manager from flask_script import Manager #导入app的函数 from myapp import get_app #创一个变量来接受get_app()返回的app app1 = get_app() #实例化manager test = Manager(app=app1) #主函数 用实例化manager去跑...原创 2018-12-03 11:25:55 · 89 阅读 · 0 评论 -
Flask---模型关系:
模型关系: 一对多: 在models中: 创建一个一的类: #创建一个一的类 class Grade(db.Model): id = db.Column( db.Integer, primary_key=True, autoincrement=True, ) name = db.Column( db.Strin...原创 2018-12-03 11:25:24 · 213 阅读 · 0 评论 -
Flask---缓存:
缓存: 配置: 在settings.py中: CACHES = { "default": { #缓存存在redis中 "CACHE_TYPE": "redis", #redis路径,存在7号库 "CACHE_REDIS_URL": "redis://127.0.0.1:6379/7", }, "debug":原创 2018-12-03 11:24:24 · 324 阅读 · 0 评论 -
Flask---钩子函数、bootstap、debugtoolbar
钩子函数: @blue.before_request def heheda(): #预处理 。。。。。。 @blue.before_request 在每次请求之前执行. 通常使用这个钩子函数预处理一些变量, 视图函数可以更好调用 flask-bootstrap: 作用: 让我们直接继承bootstrap中的东西 配置: 在ext.py中: #导包 from flask_bootstrap...原创 2018-12-03 11:23:54 · 151 阅读 · 0 评论 -
Flask---分页:
分页: 在views中: @blue.route("/") def index(): #解析get请求参数 params = request.args page = int(params.get("page", 1)) per_page = int(params.get("per_page", 15)) #创建一个当前页的分页对象 #paginate("...原创 2018-12-03 11:23:19 · 100 阅读 · 0 评论 -
Flask---单例设计模式:
单例设计模式: 不管你怎么创建对象 得到都是一个 实现: 装饰器的方式 def outer(cls): instance = None def inner(*args, **kwargs): nonlocal instance if instance == None: instance = cls(*args, **kwarg...原创 2018-12-03 11:22:23 · 932 阅读 · 0 评论 -
Flask---布署:
布署: app.py或manage.py: from flask_script import Manager from myapp import create_app app = create_app("debug") manager = Manager(app) if __name__ == '__main__': manager.run() init: from flask imp...原创 2018-12-03 11:21:59 · 101 阅读 · 0 评论 -
Flask---Views:
Views: 路径route规则: @blue.route("/ab/<any(w):e>") def id(e): print(e) print(type(e)) return "ok" @blue.route("/abc/<int:e>") def id(e): print(e) print(type(e)) r...原创 2018-12-03 09:56:40 · 343 阅读 · 0 评论 -
Flask---Template
Template: 加载、渲染: def index(): template = Template(f.read()) html = template.render({"xiang": "真香"}) return html 前端: 继承 {{super}} 宏定义: {% macro create_data(data) %} <span&g...原创 2018-12-03 09:29:15 · 310 阅读 · 0 评论 -
Flask---RESTful
RESTful: 配置: 在urls_apis_v1.py中: #导包 from flask_restful import Api #实例化Api api = Api() #写一个注册api的函数 def init_api(app): api.init_app(app) 为了防止循环引用所以就不在__init__()中注册 在__init__.py中: #导包 from myapp.ur...原创 2018-12-03 09:28:25 · 416 阅读 · 0 评论 -
Flask---Models—列:
Models—列: 最常用的SQLAlchemy列类型 类型名 Python类型 说 明 Integer int 普通整数,一般是 32 位 SmallInteger int 取值范围小的整数,一般是 16 位 BigInteger int 或 long 不限制精度的整数 Float float 浮点数 Numeric decimal.Decimal 定点数 S...原创 2018-12-01 12:26:26 · 453 阅读 · 0 评论 -
Flask---用原生sql操作:
用原生sql操作: 在sql_util.py中: #导包 import pymysql class SQLTool: def __init__(self, user, pwd, host, port, db): self.client = pymysql.connect( user=user, password=pwd, ...原创 2018-12-04 11:42:18 · 4519 阅读 · 1 评论