与 css 相关的 Django 项目模板在生产环境中不起作用

Django project templates related css is not working in production

我得到以下设置:

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 admin css 和类似的 js /static/css 并且工作正常。

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

我的 css 仍然没有出现。 我的模板出现了,但没有 css/js。 我在我的项目文件夹下得到了我所有的文件夹,例如 'templates'、static'、'static-files'。

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

collectstatic 复制 路径下 STATICFILES_DIRS 中的所有内容。因此,由于您将 appname 放在那里,因此您的 css 文件将以“/static/css/main.css”结尾。您要做的可能是将设置更改为:

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

像这样更改您的设置

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' %}" />

我发现它需要 What is the most common way to configure static files in debug and production for Django 的 gunicorn 中的设置。本教程不是基于 gunicorn,但附带的关于 gunicorn 的 link 对我来说效果很好。我很感激你们试图帮助和 Whosebug 只是岩石。