如何将 javascript 或 css 文件加载到 BottlePy 模板中?
我正在尝试使用 BottlePy 返回 html 模板。这很好用。但是,如果我在 tpl 文件中插入这样的 JavaScript 文件:
<script type="text/javascript" src="js/main.js" charset="utf-8"></script>
我会收到 404 错误。 (无法加载资源:服务器响应状态为 404(未找到))
有人知道如何解决此问题吗?
这是我的脚本文件:
from bottle import route, run, view
@route('/')
@view('index')
def index():
return dict()
run(host='localhost', port=8080)
这是模板文件,位于“./views”子文件夹中。
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="js/main.js" charset="utf-8"></script>
</head>
<body>
</body>
</html>
也许它是来自开发服务器的“rootPath/js/main.js”,它在其中查找我的js文件?
文件的结构是:
app.py
-js
main.js
-views
index.tpl
谢谢。
I am trying to return a html template with BottlePy. And this works fine. But if I insert a javascript file like this in my tpl-file:
<script type="text/javascript" src="js/main.js" charset="utf-8"></script>
I get an 404 error.
(Failed to load resource: the server responded with a status of 404 (Not Found))
Does anyone know how to fix this problem?
Here is my script file:
from bottle import route, run, view
@route('/')
@view('index')
def index():
return dict()
run(host='localhost', port=8080)
And that is the template file, located in "./views" subfolder.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="js/main.js" charset="utf-8"></script>
</head>
<body>
</body>
</html>
Maybe it is the "rootPath/js/main.js" from the development server where it looks for my js-file?
The structure of the files is:
app.py
-js
main.js
-views
index.tpl
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,首先,您需要您的开发服务器实际提供
main.js
服务,否则浏览器将无法使用它。在小型 Web 应用程序中,习惯上将所有
.js
和.css
文件放在static
目录下,因此您的布局应如下所示:并不意味着需要这种精确的命名和布局,只是经常使用。
接下来,您应该为静态文件提供一个处理程序:
这实际上会将
static/
下的文件提供给浏览器。现在,最后一件事。您将 JavaScript 指定为:
这意味着
.js
的路径相对于当前页面。在您的开发服务器上,索引页面 (/
) 将在/js/main.js
中查找.js
,以及另一个页面(例如,/post/12
)将在/post/12/js/main.js
中查找它,并且肯定会失败。相反,您需要使用
get_url
函数来正确引用静态文件。您的处理程序应如下所示:在
index.tpl
中,.js
应引用为:get_url
查找具有name 的处理程序='static'
,并计算它的正确路径。对于开发服务器,这将始终是/static/
。您甚至可以在模板中对它进行硬编码,但我不推荐它,原因有两个:/static/
目录位置,则必须在整个模板中搜索它并在每个模板中修改它。Well, first, you need your dev server to actually serve
main.js
, otherwise it won't be available for the browser.It's customary to put all
.js
and.css
files under thestatic
directory in small web apps, so your layout should look like this:By no means this exact naming and layout is required, only often used.
Next, you should supply a handler for the static files:
This will actuall serve your files under
static/
to the browser.Now, to the last thing. You specified your JavaScript as:
That means the path to
.js
is relative to the current page. On you development server, the index page (/
) will look for.js
in/js/main.js
, and another page (say,/post/12
) will look for it in/post/12/js/main.js
, and will sure fail.Instead, you need to use the
get_url
function to properly reference static files. Your handler should look like this:And in
index.tpl
,.js
should be referenced as:get_url
finds a handler withname='static'
, and calculates the proper path to it. For dev server, this will always be/static/
. You can probably even hard-code it in the template, but I don't recommend it for two reasons:/static/
dir location, you'll have to search it all over your templates and modify it in every single template.这是在 Bottle Web 项目中添加 CSS/JS 等静态文件的工作方法。我正在使用 Bootstrap 和 Python 3.6。
项目结构
app.py
index.tpl
输出
Here is a working approach of adding static files like CSS/JS in Bottle web project. I am using Bootstrap and Python 3.6.
Project structure
app.py
index.tpl
Output
我认为您将文件
main.js
放在了错误的位置。请注意,此文件路径必须相对于请求的 url,而不是相对于模板的路径。所以把它放在像
template/js/main.js
这样的文件夹中是行不通的。I think you are putting the file
main.js
in the wrong location.Note that this file path must be relative to the requested url, not relative to the path to your template. So putting it in a folder like
template/js/main.js
will not work.