学堂 学堂 学堂公众号手机端

flask跳转静态html的问题该如何解决,方案是什么

lewis 1年前 (2024-03-12) 阅读数 2 #技术
这篇文章主要讲解了“flask跳转静态html的问题该如何解决,方案是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“flask跳转静态html的问题该如何解决,方案是什么”吧!

 


1. 问题描述

在使用flask开发web的时候啊,并不是所有的页面都需要template进行修饰吧,如果我们用return render_template("xxx/xxx/xxx.html")来进行页面跳转xxx.html,那么xxx.html一定是经过模板引擎(如jinja2)修饰过的,纯静态html应该没什么问题,但是如果这个静态页面使用anjularjs,静态页面代码部分{{}}会jinja2给理解成它自己的东东,这不就造成混乱了!怎么办呢?


2. 解决办法

我们可以通过在from flask import send_file
通过return send_file("xxx/xxx/xxx.html")来返回静态html,这个html不经过jinja2修饰,会达到跟直接浏览器打开一样的效果。
如下我使用蓝图的时候的代码片段

from flask import Blueprint,render_template,send_file


rally_keystone = Blueprint('rally_keystone',__name__,url_prefix='/rally/keystone')


@rally_keystone.route('/')
@rally_keystone.route('/index')
def index():
    return render_template('rally/keystone/keystone_index.html')

@rally_keystone.route('/detail')
def detail():
    return send_file("templates/rally/keystone/test.html")

“flask跳转静态html的问题该如何解决,方案是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业技术相关的知识可以关注博信网站,小编每天都会为大家更新不同的知识。
版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门