css - STATIC_ROOT 找不到我的静态文件

标签 css django django-staticfiles

我是 Django 的新手,我正在尝试找到一种方法来在我的元素中加载我的 css 文件。这是我的 settings.py 文件

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    #"django.contrib.staticfiles.finders.AppDirectoriesFinder"
)

AUTHENTICATION_BACKENDS = (
    # Uncomment the following to make Django tests pass:
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
)

我找到了一种使用变量 STATICFILES_DIRS 的方法,但它不能与其他类似变量一起使用。

我应该怎么做才能修复它?

额外信息:

我的base.html文件静态文件调用:

<link rel="stylesheet" type="text/css" href="{% static 'style.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'icomoon_style.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap.min.css' %}" />

我的文件夹处理:

  • OntoLogica(主文件夹)
    • Ontologica(元素文件夹)
    • 静态文件夹
      • icomoon_style.css
      • 样式.css
      • css文件夹
      • bootstrap.min.css

最佳答案

我找到了解决我的案子的方法。它是在这个网站上找到的:https://devcenter.heroku.com/articles/django-assets

我要做的事情:

添加 PROJECT_ROOT 行并将 STATIC_ROOT 行更改为:

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')

有了这个,我可以毫不费力地添加 STATICFILES_DIRS 行:

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

它对我来说非常有效,我希望它也能帮助其他人。谢谢

关于css - STATIC_ROOT 找不到我的静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39580757/

相关文章:

css - Django:Firefox 不使用加载的 CSS

python - 为创建 Django 模型实例添加日志记录的最佳方法是什么?

html - Bootstrap 4 : text-center & aligned left?

javascript - 如何动态改变CSS样式?

css - 不知道我们如何从导入的组件中删除填充

python - Django 从外部源接收 json post 请求

python - 在 Django 管理屏幕中删除 "add another"

django - 如何使用 Amazon 的 S3 配置 django-compressor 和 django-staticfiles?

django - 如何仅使用 collectstatic 忽略特定应用程序的静态文件?

css - 避免重叠具有相同网格区域的元素