编辑:这个问题是由 skelJS 而不是 Django 引起的。查看答案。
我是 Django 的新手,但已经阅读了文档、Google 和几个 StackOverflow 主题,但似乎无法让静态文件按预期工作。
当我的模板被渲染时,静态路径被正确插入(例如 {{ STATIC_URL }}css/style.css 被转换成 static/css/style.css。我能够导航到 http://127.0.0.1:8000/static/css/style.css
在我的浏览器中。
CSS 部分工作,但未呈现 CSS 文件中引用的图像。但是,我可以毫无问题地导航到 http://127.0.0.1:8000/static/css/images/bg02.jpg
(正文背景图片)。出于好奇,我运行了 manage.py collectstatic,它用 css/、js/和 image/填充了我的静态文件夹,将我的(非 Django 模板化的)index.html 复制到该目录,打开它,一切都正确呈现。此外,在 style.css 中,我使用查找和替换将所有出现的 images/*.jpg
更改为 static/css/images/*.jpg
,css/images/*.jpg
,只是 *.jpg
并不令人满意(我还通过访问 http://127.0 确认了这些更改.0.1:8000/static/css/style.css
在我的浏览器中)。
如果您能提供任何帮助,我们将不胜感激。
父模板 base_home.html
{% load staticfiles %}
...
<script src="{{ STATIC_URL }}js/jquery.min.js"></script>
<script src="{{ STATIC_URL }}js/config.js"></script>
<script src="{{ STATIC_URL }}js/skel.min.js"></script>
<script src="{{ STATIC_URL }}js/skel-panels.min.js"></script>
<noscript>
<link rel="stylesheet" href="{{ STATIC_URL }}css/skel-noscript.css" />
<link rel="stylesheet" href="{{ STATIC_URL }}css/style.css" />
<link rel="stylesheet" href="{{ STATIC_URL }}css/style-desktop.css" />
</noscript>
<!--[if lte IE 9]><link rel="stylesheet" href="{{ STATIC_URL }}css/ie9.css" /><![endif]-->
<!--[if lte IE 8]><script src="{{ STATIC_URL }}js/html5shiv.js"></script><![endif]-->
...
子模板 home.html(由 View 调用,扩展 base_home.html)
#This is pretty plain, but I wanted to add/learn the functionality early
{% extends "base_home.html" %}
{% block headline %}Insert catchy headline here.{% endblock %}
呈现的 HTML
<script src="/static/js/jquery.min.js"></script>
<script src="/static/js/config.js"></script>
<script src="/static/js/skel.min.js"></script>
<script src="/static/js/skel-panels.min.js"></script>
<noscript>
<link rel="stylesheet" href="/static/css/skel-noscript.css" />
<link rel="stylesheet" href="/static/css/style.css" />
<link rel="stylesheet" href="/static/css/style-desktop.css" />
</noscript>
<!--[if lte IE 9]><link rel="stylesheet" href="/static/css/ie9.css" /><![endif]-->
<!--[if lte IE 8]><script src="/static/js/html5shiv.js"></script><![endif]-->
settings.py
...
STATIC_ROOT = 'E:/Projects/sandbox/website/static'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'E:/Projects/sandbox/website/resources',
# This dir contains css/ images/ and js/
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.request",
"django.contrib.auth.context_processors.auth",
'django.core.context_processors.static',
)
...
views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from sandbox.context_processors import sitevars
def home(request):
return render_to_response("home.html", context_instance=RequestContext(request, processors=[sitevars]))
context_processors.py
def sitevars(request):
return {
'meta_d': 'Meta description here',
'meta_k': 'Meta keywords here',
'title': 'HTML page title here',
'logo': 'Text that needs to be inserted on pages',
'copyright': r'HTML/text that needs to be inserted on pages'
}
样式.css
body
{
background: #D4D9DD url('images/bg02.jpg');
}
...
.check-list li
{
...
background: url('images/icon-checkmark.png') 0px 1.05em no-repeat;
}
...
.bordered-feature-image
{
...
background: #fff url('images/bg04.png');
}
...
.custom-feature-image
{
...
background: #fff url('images/bg05.png');
}
...
.item header
{
...
background: #2b3336 url('images/bg01.jpg');
}
最佳答案
好的,伙计们,我是对的,这与 Django 完全无关,对此我深表歉意。
对于任何使用 skelJS 的读者...
代码美化/去最小化后,寻找:
newStyleSheet: function (a) {
var c = document.createElement("link");
c.rel = "stylesheet";
c.type = "text/css";
c.href = a;
return c
},
然后,将 c.href = a;
替换为 c.href = "/static/"+ a;
,其中 "/static/"是您在中定义的 STATIC_URL设置.py.
关于javascript - Django + skelJS/静态文件问题/CSS 中对图像的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19145031/