Python 官方文档:入门教程 => 点击学习
安装模板引擎Jinja2MarkupSafe-0.23.tar.gzJinja2-2.8.tar.gz2.配置模板引擎到blueprint/home/WEBapp |-- main.py |-- my_blueprint.p
安装模板引擎Jinja2
MarkupSafe-0.23.tar.gz
Jinja2-2.8.tar.gz
2.配置模板引擎到blueprint
/home/WEBapp
|-- main.py
|-- my_blueprint.py
templates
|-- index.html
1).main.py
from sanic import Sanic
from my_blueprint import bp
app = Sanic(__name__)
app.blueprint(bp)
app.run(host='0.0.0.0', port=8000, debug=True)
2).my_blueprint.py
# more my_blueprint.py
from sanic.response import JSON, text, html
from sanic import Blueprint
from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('my_blueprint', 'templates'))
bp = Blueprint('my_blueprint')
@bp.route('/')
async def bp_root(request):
template = env.get_template('index.html')
content=template.render(title='Sanic',people='David')
return html(content)
#
3).index.html
# more index.html
<!doctype html>
<title>` title `</title>
<div class=page>
<h1>hello, ` people `</h1>
</div>
3.运行测试
运行服务
/home/webapp/目录下
[root@localhost webapp]#
[root@localhost webapp]# python main.py
浏览器访问网址 Http://192.168.199.201:8000/
--结束END--
本文标题: python 3.5 sanic web
本文链接: https://www.lsjlt.com/news/186939.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0