Python-Flask-2023.1.24-Review
路由
@app.route('') add_url_rule 的方法 在底层进行绑定 用了这个装饰器的方法
def test():
pass
路由可以返回的值(str, int, float, path, uuid(用户唯一标识))
3、对于Flask来说,所有的视图函数 必须都要有返回值, 不能为空 的
4、视图函数:
返回值:
类型:字符串,dict,tuple,response,WSGI(Web Server Gateway Interface)
response对象 响应对象
response('字符串', headers={key:value})
response = make_response('......')
response.headers['aaa] = 'abc'
一般的是默认: 字符串为 返回对象
request对象
request.path
request.full_path
重点:
request.args:一般是在get请求下去使用 但是在导航栏上 是可以 看到 输入 相关消息的 表 单提交相关不会使用get方法的
request.form:一般是在post请求下使用
response响应
1.1 str ------->自动转换成response对象
1.2 dict ------->转换成一个json的对象
1.3 response对象--------> response对象
1.4 make_response() ------->response对象
1.5 redirect() -------> 重定向 获得302的状态码
1.6 render_template()---------> 模板渲染 将那个模板引进来了
5、模板
模板的语法:render_template
5.1、在模板中获取view中床底的变量值:{{变量名}}
render_template('模板名字', key)
name ='faker' # str
age = 18 # int
friends = ['bang','uzi', 'meiko'] #list
dict1 = {'gift1': s1, 'gift2':s2, 'gift3':s3} # dict
girlfriend = Girl('faker', 'Korean') # 自定义的类构建的类型:Girl对象
模板:
{{ list.0 }} <==>{{ list[0] }}
{{ dict.key }}<==>{{ dict.get(key) }}
{{ girl.name }}<==>{{ 对象.属性 }}
5.2 、控制块
{% if 条件 %}
{% endif %}
{% if 条件 %}
条件为True
{% else %}
条件为Flase
{% endif %}
{% for 变量 in 可迭代的对象 %}
for 循环要做的任务
{% endfor %}
可以使用loop变量 loop.index -----> 写上序号 loop.index0 -----> 序号是从0开始的
5.3、 loop.revindex ------> 作用等价于 reverse 序号倒着的 5 4 3 2 1
loop.first -----> 布尔类型 检查是否是第一行
loop.last ------> 布尔类型 检查是否是第二行