网站建设的er图,成都网站设计是什么,超便宜的网站,网站建设营销方案渲染模版就是在 Flask_RESTful 的类视图中要返回 html 片段代码#xff0c;或 者是整个html 文件代码。 如何需要浏览器渲染模板内容应该使用 api.representation 这个装饰器来定 义一个函数#xff0c; 在这个函数中#xff0c;应该对 html 代码进行一个封装#xff… 渲染模版就是在 Flask_RESTful 的类视图中要返回 html 片段代码或 者是整个html 文件代码。 如何需要浏览器渲染模板内容应该使用 api.representation 这个装饰器来定 义一个函数 在这个函数中应该对 html 代码进行一个封装再返回。 注意 api.representation 装饰器修饰的函数必须返回一个 Response 对象 main.py
from flask import Flask,render_template,Response
from flask_restful import Api,Resource
import json
app Flask(__name__)app.config[RESTFUL_JSON] dict(ensure_asciiFalse)api Api(app)class HomeView(Resource):def get(self):return {msg:这个是个人主页}class IndexView(Resource):def get(self):return render_template(index2.html)api.add_resource(IndexView,/index/)
api.add_resource(HomeView,/home/)api.representation(text/html)
def out_html(data,code,headers):# 必须返回一个response对象if isinstance(data,code,headers):resp Response(data)return resp else:return Response(json.dumps(data,ensure_asciiFalse)).encode(gbk)if __name____main__:app.run(debugTrue)index2.html
!DOCTYPE html
html langen
headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title/title
/head
bodyh1这是个模板内容/h1
/body
/html
执行结果