pythonflask_Python的flask模块方法解释(用到一个更新一个)

一、render_template:模板渲染

代码:render_template('hello.html', name=name)

解释:渲染html模板,并且传递参数name

二、session:设置、编辑、删除session

具体使用方法参见:python的flask操作设置、获得与删除session(http://www.zongk.com/zongk/102.html)

三、redirect:跳转重定向

代码:redirect(url_for('login'))

解释:

flask.redirect(location, code=302)

Return a response object (a WSGI application) that, if called, redirects the client to the target location. Supported codes are 301, 302, 303, 305, and 307. 300 is not supported because it’s not a real redirect and 304 because it’s the answer for a request with a request with defined If-Modified-Since headers.

0.6 新版功能: The location can now be a unicode string that is encoded using the iri_to_uri() function.

参数:

location – the location the response should redirect to.

code – the redirect status code. defaults to 302.

常用代码:利用url_for反转得到路径:redirect(url_for('login'))或者直接给页面路径:redirect('/login/'),自补优缺点

四、构造URL链接:url_for

解释:根据方法反转得到URL

看图:

url_for 静态文件用法:

url_for('static', filename='style.css')

可以再模板引用CSS、JS、图片等静态文件

五、获得HTTP信息:request

这里可以参考几篇之前发的文章:

1、python的flask 获取GET与POST请求数据(http://www.zongk.com/zongk/103.html)

说明:这里手册可以到:官方手册内的:Werkzeug 文档

样例:

1、request.method:获得请求方法例如:GET 或者POST

2、request.form['username'] :获得POST请求username数据

六、make_response:自理解将模板转成一个可操作的对象,设置类似cookies信息

可以查看文章:

1、Python的flask框架设置cookies与获取cookies(http://www.zongk.com/zongk/101.html)

其他用法举例(来自flask手册):

如果你想在视图里操纵上述步骤结果的响应对象,可以使用 make_response() 函数。

譬如你有这样一个视图:

@app.errorhandler(404)

def not_found(error):

return render_template('error.html'), 404

你只需要把返回值表达式传递给 make_response() ,获取结果对象并修改,然后再返回它:

@app.errorhandler(404)

def not_found(error):

resp = make_response(render_template('error.html'), 404)

resp.headers['X-Something'] = 'A value'

return resp

七、(Jinja2)模板继承:extends

使用代码:{% extends 'muban.html' %}

解释:

继承一个前端的主框架HTML模板(以下简称‘母板’),这段代码出现index.html文件头表示他继承了母板前端的布局框架即:'muban.html',使用jinja的块‘block’填充内容,或者使用变量{{ 变量名 }}填充内容。

jinja样例代码:

{% block title %}{% endblock %}

母板:muban.html内同样使用‘block’对应位置填充进母板进行渲染展示,样例代码截图展示(一部分):

这个是子板index.html内的block

这里是母板muban.html内对应位置的填充渲染图:

八、……

学习爱好者,共同学习、交流分享,享受'玩'中乐趣

来自Davids zhou博客原创文章请尊重作者:http://www.zongk.com/zongk/104.html转载请标注此链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值