1

我有以下设置:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static', 'appname'), )

在我的模板中,我得到了

 <link rel="stylesheet" href"{% static 'appname/css/main.css' %}" />
  1. 我的静态文件夹包括默认的 Django 管理 css 和 js,例如 /static/css 并且它工作正常。

  2. 我的同一个静态文件夹包括 /static/appname/css/main.css

我的CSS仍然没有出现。我的模板出现了,但没有 css/js。我在我的项目文件夹下获得了所有文件夹,例如“模板”、“静态”、“静态文件”。

我的默认 Django 静态文件显示在 static/admin 下的“coollectstatic”之后。但我不知道如何在生产中使用“STATICFILES_DIRS”来显示我的项目模板相关的静态文件。我正在部署生产。请指教。

4

3 回答 3

1

我发现它需要从What is the most common way to configure static files in debug and production for Django中的 gunicorn 中进行设置。本教程不是基于 gunicorn,但附加的有关 gunicorn 的链接对我来说效果很好。我很感激你们试图提供帮助,stackoverflow 只是摇滚。

于 2016-02-10T11:57:58.257 回答
0

collectstatic copies everything below the paths in STATICFILES_DIRS. So since you are putting appname there your css file will end up at "/static/css/main.css". What you want to do is probably change the setting to:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
于 2016-02-10T08:50:52.930 回答
0

像这样更改您的设置

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
    os.path.join(BASE_DIR, 'static', 'appname'), 
)

现在您可以像这样加载静态文件

{% load staticfiles %}
<link rel="stylesheet" href"{% static 'appname/css/main.css' %}" />
于 2016-02-10T08:54:02.003 回答