Django静态文件未find值错误

我正在使用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构build过程中没有报告错误,之后运行collectstatic。 服务器上的文件的权限设置为775。

在base.pyconfiguration中的静态configuration:

 # 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 %} 

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

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

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