如何使用Flask中的"url_for"指令正确设置,以便使用Bootstrap和RGraph的html页面有效?
说我的html页面看起来像这样(部分片段): -
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="scripts/bootstrap/dist/css/bootstrap.css" rel="stylesheet">
<title>HP Labs: Single Pane Of Glass (Alpha)</title>
<script src="scripts/RGraph/libraries/RGraph.common.core.js" ></script>
<script src="scripts/RGraph/libraries/RGraph.line.js" ></script>
<script src="scripts/RGraph/libraries/RGraph.common.effects.js" ></script>
<script src="scripts/RGraph/libraries/RGraph.line.js" ></script>
......
</html>
Run Code Online (Sandbox Code Playgroud)
这是我做/想做的事情: -
在我的Flask模块旁边创建了一个"模板"目录,并将这个html文件放在其中.
在我的Flask模块旁边创建了一个"静态"目录,但不确定要使用的"url_for"类型语句的位置和数量以及它们应该去的位置.所以目前"scripts"目录是"templates"目录中的子目录(我知道这是不正确的).
我希望能够正确引用所有Bootstrap和RGraph js和css(现在看到很多404).
任何人都可以指示我正确配置Flask(运行开发服务器)来执行此操作吗?现在js和css不起作用.
谢谢 !
Mar*_*ers 13
将scripts目录放在static子目录中,然后使用:
<link href="{{ url_for('static', filename='scripts/bootstrap/dist/css/bootstrap.css') }}" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)
这里的模式是:
{{ url_for('static', filename='path/inside/the/static/directory') }}
Run Code Online (Sandbox Code Playgroud)
即使您将所有这些文件切换到不同的托管(如CDN),也将替换为静态资源的正确URL.