python 3.5 sanic web framework template with jinga2

简介:
  1. 安装模板引擎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 


wKiom1hOUIqC4FskAAKowjhQOjU050.jpg-wh_50



浏览器访问网址 http://192.168.199.201:8000/



wKioL1hOTwTALBGEAABdymxWWGQ875.jpg-wh_50


本文转自 pgmia 51CTO博客,原文链接:http://blog.51cto.com/heyiyi/1881950


相关文章
|
10月前
|
数据采集 Python
matlab疲劳驾驶检测项目,Python高级面试framework
matlab疲劳驾驶检测项目,Python高级面试framework
|
10月前
An error happened during template parsing (template: “ServletContext resource [/WEB-INF/templates/in
An error happened during template parsing (template: “ServletContext resource [/WEB-INF/templates/in
|
10月前
|
JSON API 网络架构
Python Web 开发: 解释 Django REST framework 的作用,以及如何定义序列化器(Serializer)?
Python Web 开发: 解释 Django REST framework 的作用,以及如何定义序列化器(Serializer)?
109 0
|
SQL 安全 Java
Web Security 之 Server-side template injection
Web Security 之 Server-side template injection
107 0
|
JSON 数据库 数据格式
[Python]Django 视图(View)和URL 与 模板(Template)
[Python]Django 视图(View)和URL 与 模板(Template)
|
前端开发 JavaScript 数据库
Django框架开发005期 Python调用template模板网页开发调用静态资源
Django框架开发005期 Python调用template模板网页开发调用静态资源
139 0
|
Python
Django框架开发004期 Python编程调用自定义Django框架template模板网页
Django框架开发004期 Python编程调用自定义Django框架template模板网页
168 0
An error happened during template parsing (template: “ServletContext resource [/WEB-INF/templates/in
学习An error happened during template parsing (template: “ServletContext resource [/WEB-INF/templates/in
226 0
An error happened during template parsing (template: “ServletContext resource [/WEB-INF/templates/in
|
机器人 Linux C语言
Linux CentOS下Python+robot framework环境搭建
Linux CentOS下Python+robot framework环境搭建
293 0
|
存储 JSON 缓存
python flask template 模板应用
python flask template 模板应用
python flask template 模板应用

热门文章

最新文章