在django 1.8中如何链接到站点范围的静态文件? [英] How do you link to site-wide static files in django 1.8?

查看:119
本文介绍了在django 1.8中如何链接到站点范围的静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前结构:

MySite
  |--MySite
  |    |--static
  |         |--MySite
  |              |-- site_wide.css
  |--MyApp
       |--static
            |--MyApp
                 |-- app_specific.js
                 |-- app_specific.css

settings.py:

settings.py:

STATIC_URL = "/static/"

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "/MySite/static/"),
)

使用以下代码,我可以成功链接到 app_specific.js app_specific.css ,但无法链接 site_wide.css

With the following code I am able to successfully link to app_specific.js and app_specific.css, but am unable to link site_wide.css

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'MyApp/app_specific.css %}" />
<link rel="stylesheet" type="text/css" href="{% static 'MySite/site_wide.css' %}" />

... site body ...

<script type="text/javascript" src="{% static 'MyApp/app_specific.js' %}" ></script>

在检查生成的html时,第二个链接的href是:

Upon inspecting the generated html, the href for the 2nd link is:

href="/static/MySite/site_wide.css"

不存在。

我目前的理解是,django将在由 STATIC_URL 然后(如果没有找到)通过 SATICFILES_DIRS 中列出的路径进度,但是显然从未查看 STATICFILES_DIRS

My current understanding is that django would search for my linked file in the path defined by STATIC_URL and then (if not found) progress through the paths listed in SATICFILES_DIRS, however it is apparently never looking at the path specified in STATICFILES_DIRS.

我是django的全新,想了解它是如何工作的。一些其他类似的问题提到了一些类似 collectstaticfiles 或类似的东西,但都非常不清楚。

I am brand new to django and want to understand how it works. Some other similar questions mention things like collectstaticfiles or something like that but are all very unclear.

任何帮助将非常感谢!

PS我知道的一切都是从Django官方的1.8教程/ docs,所以请不要发表弃用的答案。

P.S. Everything I know is from the official Django 1.8 tutorial/docs, so please do not post deprecated answers. 1.8 only.

推荐答案

尝试更改 os.path.join(BASE_DIR,'/ MySite / static /'c $ c> to os.path.join(BASE_DIR,'MySite / static'),i。即删除转发 / 。我想原因是如何 os.path.join 工作

Try to change os.path.join(BASE_DIR, '/MySite/static/') to os.path.join(BASE_DIR, 'MySite/static'), i. e. remove forwarding /. I guess the reason is how os.path.join works

>>> os.path.join('/foo', '/bar')
'/bar'
>>> os.path.join('/foo', 'bar')
'/foo/bar'

这篇关于在django 1.8中如何链接到站点范围的静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆