forked from mizhiX/django-teaching-15days
-
Notifications
You must be signed in to change notification settings - Fork 0
/
笔记.txt
107 lines (63 loc) · 1.39 KB
/
笔记.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
1. 宏定义macro
{% from 'fucntion.html' import hello %}
2. for
{% for i in list1 %}
{{ i }}
{% endfor %}
3. if
ifequal 在jinja2中没有
{% if i == 1 %}
{% else %}
{% endif %}
3. loop
forloop.counter
forloop.counter0
forloop.revconter
forloop.revconter0
序号: loop.index
反转: loop.revindex
第一次:loop.fitst
最后一次:loop.last
4. 过滤器
safe:将标签渲染
striptags:将标签去掉
length:计算长度
capitalize: 首字母大写
lower:小写
upper:大写
5. sqlalchemy配置
第一步:定义模型
db = SQLALchemy()
class Student(db.Model):
pass
primary_key = True
autoincrement = True
nullable = True
Integer
String(20)
第二步: 初始化db和app
db.init_app(app)
第三步: 马上配置数据库
SQLALCHEMY_DATABASE_URI: mysql+pymysql://root:password@127.0.0.1:3306/flask3
SQLALCHEMY_TRACK_MODIFICATIONS = Fasle
第四步:
db.create_all()
6. CRUD
增:
db.session.add()
db.session.commit()
查:
模型.query.all() ---> list
模型.query.filter(模型.字段==字段值) ---> basequery
模型.query.filter(模型.字段==字段值).all() ---> list
模型.query.filter_by(字段=字段值)
删:
db.session.delete(对象)
db.session.commit()
改:
对象 = 模型.query.get(id)
对象.字段 == 字段值
db.session.add(对象)
db.session.commit()
模型.query.filter().update({‘’:‘’})
db.session.commit()