找不到Django静态文件值错误

时间:2017-09-09 16:20:03

标签: python django docker nginx docker-compose

我正在使用cookiecutter django模板开发一个项目:https://github.com/pydanny/cookiecutter-django 该项目在docker容器中运行,该容器随ubuntu 16.04LTS上的cookiecutter-django模板一起提供。

尝试将网站投入生产时,会在某些页面上返回以下错误:

the file 'events\css\themes\fontawesome-stars.css' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x7f830be38ac8>.

文件夹结构是:

./project/events/static/
└── events
    ├── css
        ├── details.css
        ├── list.css
        └── themes
            ├── fontawesome-stars.css
            └── fontawesome-stars-o.css

在docker构建过程中以及运行collectstatic之后,不会报告任何错误。 服务器上文件的权限设置为775。

base.py config中的

静态配置:

# STATIC FILE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = str(ROOT_DIR('staticfiles'))

# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = '/static/'

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = [
    str(APPS_DIR.path('static')),
]

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders
STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

在模板中,我包含了这样的文件。:

{% load static %}
{% load crispy_forms_tags %}
{% block title %}
{% endblock%}

{% block css %}
{{block.super}}
<link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="{% static 'events\css\themes\fontawesome-stars.css' %}">
{% endblock %}

1 个答案:

答案 0 :(得分:1)

您如何在模板中包含静态文件?看起来你直接指定了路径。相反,你应该使用:

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'events/css/themes/fontawesome-stars.css' %}">

因为在生产中whitenoisecollectstatic命令会在文件名中添加额外的内容,以用于版本控制,缓存和其他目的。