简体   繁体   English

Django:css引用静态文件中的媒体(django dev / 1.3 /静态文件)

[英]Django: css referencing media in static files (django dev / 1.3 / static files)

Like any other user of django user I serve static files. 像django用户的任何其他用户一样,我提供静态文件。 I've chosen to use django-staticfiles to be ready for django 1.3 which will basically integrate it into the core . 我选择使用django-staticfiles为django 1.3做好准备,它基本上将它集成到核心中

My question is pretty simple really - this works great for pulling together multiple media sources and referencing them in a uniform way in django templates. 我的问题非常简单 - 这非常适合将多个媒体源整合在一起并在django模板中以统一的方式引用它们。 However, I often use image backgrounds in Css like so: 但是,我经常在Css中使用图像背景,如下所示:

#itemname { background-image: url('/path/to/image.png'); }

My question is simple - if I use absolute names, I have to hard code them. 我的问题很简单 - 如果我使用绝对名称,我必须对它们进行硬编码。 If I use relative names, moving to "subdirectory" urls messes up the resource location for these items and they can't be loaded. 如果我使用相对名称,移动到“子目录”URL会弄乱这些项目的资源位置,并且无法加载它们。

So, how do I extend this solution to CSS? 那么,我该如何将此解决方案扩展到CSS? Said solution must avoid: 所述解决方案必须避免

  • Embedding css in html . 在html中嵌入css I personally avoid this. 我个人避免这个。
  • Using hardcoded urls . 使用硬编码的网址 This does not work very well because on my local setup I typically use 'localhost/project' with apache for testing (mod_wsgi) whereas I tend to use project.com for deployment. 这不能很好地工作,因为在我的本地设置中,我通常使用'localhost / project'和apache进行测试(mod_wsgi),而我倾向于使用project.com进行部署。

Ideas? 想法?

You said you had trouble with relative paths, but I don't understand exactly what you meant. 你说你有相对路径的问题,但我不明白你的意思。

I ran into the same issue, and I've used relative paths to solve it. 我遇到了同样的问题,我用相对路径来解决它。 The only thing to keep in mind is that when deploying the images need to (obviously) remain in the same path relative to the CSS files. 唯一要记住的是,部署图像时(显然)需要保持相对于CSS文件的相同路径。

My setup in a nutshell: 我的设置简而言之:

Note I'm still using django-staticfiles with Django 1.2, but it should work similarly for Django 1.3 注意我仍在使用Django 1.2的django-staticfiles,但它应该与Django 1.3类似

STATIC_URL = "/site_media/static/"
STATIC_ROOT = os.path.join(PROJECT_ROOT, "site_media", "static")
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, "static_media"),
)

Then I serve the CSS from {{ STATIC_URL }}css/style.css which references images at ../images/logo.png . 然后,我服务从CSS {{ STATIC_URL }}css/style.css其在引用图像../images/logo.png

and my project looks like this: 我的项目看起来像这样:

project_dir
  ...
  stuff
  static_media
    ...
    css
    images

Let me know if you have any questions, and I'll clarify. 如果您有任何问题,请告诉我,我会澄清一下。

Ok, 好,

I don't know if there is something wrong with @John's solution but it didn't worked to me then I put this code on the CSS 我不知道@ John的解决方案是否有问题,但它对我不起作用然后我把这个代码放在CSS上

{% load static %}
{% get_static_prefix as STATIC_PREFIX %}

and

<link rel="stylesheet" href="{{ STATIC_PREFIX }}css/main.css">

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM