当通过子 URI 提供静态文件时,找不到更少的 css 背景图像

发布于 2024-12-27 17:49:19 字数 1479 浏览 1 评论 0原文

对于那些感兴趣的人,我正在运行 Django 1.3 设置,利用可爱的静态文件集成和 less.js。

我遇到的问题是这样的:

在我的 HTML 中,我有:

<link rel="stylesheet/less" type="text/css" href="{{ STATIC_URL }}less/style.less">
<script src="{{ STATIC_URL }}js/less.js" type="text/javascript"></script>

在这种情况下 STATIC_URL 是 /static/ 因此,当处理 HTML 时,链接分别变为 /static/less/style.less 和 /static/js/less.js 。

现在,在我的 LESS 文件中,我有以下(简化的)代码:

body {
    background: url('../img/bkgnd.png');
}

问题是 LESS 对我来说似乎愚蠢地处理了这个问题,除非我很愚蠢,就像可能一样:-)

less JS 文件被抓取为 http://localhost:8000/static/js/less.js 然后抓取css文件在 http://localhost:8000/static/less/style.less

现在我希望更少应该知道 style.less 的位置,因此在编译为 CSS 时会在 http://localhost:8000/static/less/../img/bkgnd.png 显然只是 http://localhost:8000/static/img/backgnd.png

然而,它似乎忽略了该位置,只是在 http://localhost:8000/img/backgnd.png

有谁有解释为什么会发生这种情况?

我通过设置包含静态 url 的 javascript 变量并将其添加到 css 文件中的图像 url 前面来添加临时修复。这对于开发来说很好,但我想在生产中使用 lessc 来编译代码,而 less 不允许我传递 javascript 变量,据我所知(这会很好)。

任何帮助将不胜感激,这让我发疯:-)

For those that are interested I'm running a Django 1.3 setup making use of the lovely static files integration and less.js.

The issue I am running into is this:

In my HTML I have:

<link rel="stylesheet/less" type="text/css" href="{{ STATIC_URL }}less/style.less">
<script src="{{ STATIC_URL }}js/less.js" type="text/javascript"></script>

In this case STATIC_URL is /static/ so when the HTML is processed the links become /static/less/style.less and /static/js/less.js respectively.

Now in my LESS file I have the following (simplified) code:

body {
    background: url('../img/bkgnd.png');
}

The problem is that LESS to me appears to deal with this stupidly, unless i'm being stupid, just as likely :-)

The less JS file is grabbed as http://localhost:8000/static/js/less.js and then grabs the css file at http://localhost:8000/static/less/style.less

Now I would expect that less should know the location of style.less and therefore when compiling to CSS would look for the background image at http://localhost:8000/static/less/../img/bkgnd.png which obviously is just http://localhost:8000/static/img/backgnd.png

Instead however it seems to ignore the location and just look for the background image at http://localhost:8000/img/backgnd.png.

Does anyone have an explanation why this happens?

I have added a temporary fix by setting a javascript variable containing the static url and prepending it to the image url in the css file. This is fine for development, but I want to compile the code using lessc in production and less doesn't allow me to pass in javascript variables as far as I can see (which would be nice).

Any help would be greatly appreciated, this is driving me crazy :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由如风 2025-01-03 17:49:19

它正在寻找与您正在查看的文档相关的 img 文件,而不是与 css 文件相关的文件。

因此,将你的 css 更改为:

body {
   background: url('/static/img/bkgnd.png');
}

It's looking for a img file in relations to the document you are viewing, not in relation to the css file.

So change your css to this:

body {
   background: url('/static/img/bkgnd.png');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文