2016-02-19 198 views
7

這個問題似乎被問了幾次,但我無法修復它。在Heroku上沒有找到Django靜態文件(帶有whitenoise)

我在DEBUG = False的生產環境中部署了一個django應用程序。我設置了我的allowed_host。 我用{% load static from staticfiles %}加載靜態文件。我究竟寫的Heroku的文檔sugested設置:

BASE_DIR = os.path.dirname(os.path.dirname(__file__)) 
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) 

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles') 
STATIC_URL = '/static/' 

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

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' 

但我得到了一個500錯誤,當我運行heroku run python manage.py collectstatic --noinput 所有似乎確定了這個回溯(郵寄)

... 
`cache_name = self.clean_name(self.hashed_name(name)) 
File "/app/.heroku/python/lib/python3.5/site- packages/django/contrib/staticfiles/storage.py", line 94, in hashed_name (clean_name, self)) 
... 
ValueError: The file ‘app/css/font.css’ could not be found with <whitenoise.django.GzipManifestStaticFilesStorage object at 0x7febf600a7f0>.` 

276 static files copied to '/app/annuaire/staticfiles', 276 post-processed.

有沒有人有想法來幫助我?

感謝

編輯:

annuaire 
|-- /annuaire 
|-- -- /settings.py 
|-- /app 
|-- -- /static/...` 

wsgi.py

from django.core.wsgi import get_wsgi_application 
from whitenoise.django import DjangoWhiteNoise 


application = get_wsgi_application() 
application = DjangoWhiteNoise(application) 
+1

的Django通常只要求當DEBUG =假,所以我困惑的是,你可以得到與調試錯誤特定功能=真。我建議用DEBUG = False在本地測試你的應用程序,運行'collectstatic'然後運行'runserver',看看你是否得到這個錯誤。難道你只是沒有犯這個特定的文件? –

+0

對不起!當調試是錯誤的,你是對的! – vpoulain

+0

您是否打印過'STATIC_ROOT'以確保它與'/ app/annuaire/staticfiles''相同? – agconti

回答

1

爲BASE_DIR你需要一個雙目錄名,如果你的設置是不是根,但/項目名/文件夾:

settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 
STATIC_URL = '/static/' 
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') 
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'), 
) 
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' 
# for /static/root/favicon.ico  
WHITENOISE_ROOT = os.path.join(BASE_DIR, 'staticfiles', 'root') 

template.html

{% load staticfiles %} 
<link rel="stylesheet" href="{% static "app/css/font.css" %}"> 

這個示例應用程序樹:

annuaire 
|-- /annuaire 
|-- -- /settings.py 
|-- /app 
|-- /static/app/css/font.css 
+0

謝謝!我做到了,但仍然無法工作......我在主要問題帖子中編輯了我的django應用程序樹。 – vpoulain

+0

這真是奇怪,當我collectstatic我有'275靜態文件複製到'/ app/staticfiles',1未修改,276後處理。「但在heroku bash:staticfiles仍然是空的... – vpoulain

+0

@vpoulain那是因爲每個新Heroku進程運行在自己的臨時文件系統中,因此新文件不會被其他進程看到。 (請參閱https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem)。在最初推送文件的過程中,必須運行「collectstatic」。 –

4

我知道了。我需要添加 python manage.py collectstatic --noinput; 在我的Procfile中。 Heroku doc表示collecticstatic被自動觸發。 https://devcenter.heroku.com/articles/django-assets

感謝

+0

或通過[儀表板](https://id.heroku.com/login)從應用程序設置中刪除環境變量DISABLE_COLLECTSTATIC。重要的是:不要把它的值設爲0,只要徹底刪除這個env變量。這樣你就不必編輯你的'Procfile'。然後,所有人都應該工作(至少他們爲我工作!)。 積分轉到:[YunoJuno](http://tech.yunojuno.com/django-whitenoise-and-heroku-s-ephemeral-filesystem)。 –

1

隨着DEBUG=False,什麼原件使用工作不爲我工作了。

但是通過啓用whitenoiseMIDDLEWAREsettings.py的修復解決了它。最好不要在SecurityMiddleware以下。

MIDDLEWARE = [ 
    'django.middleware.security.SecurityMiddleware', 
    'whitenoise.middleware.WhiteNoiseMiddleware', # add this line 
    #Other middleware... 
] 

```

按照docs,它實際上需要在第一時間被激活。

0

問題是,Heroku中的Python應用程序使用內置的Web服務器並且不提供靜態文件。

您可以使用whitenoise的應用程序,這個工作解決方案是100%。

假設你已經生成靜態文件,例如:

$ python manage.py collectstatic 

接下來你需要做的是:

1)$ pip install whitenoise

2)添加字符串「白噪聲== 3.3。 0「in your requirements.txt

3)在設置中添加代碼。 PY

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') 

4)應用添加此代碼/ wsgi.py

from whitenoise.django import DjangoWhiteNoise 
application = DjangoWhiteNoise(application) 
0

除了上述的答案,它也可以是你沒有指定一個正確的STATIC_ROOT爲描述在https://docs.djangoproject.com/en/2.0/howto/static-files/#deployment

對我來說,解決方案是將此添加到我的生產設置的結尾.py

STATIC_ROOT = "/app/static/" 

要知道你的靜態文件夾在Heroku上運行此

heroku run python manage.py collectstatic 

然後你會看到正在那裏所示的路徑。

0

對於我下面的工作。

settings.py

DEBUG = True 

STATIC_URL = '/static/' 
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') #this is not used 
# Add static folder to STATIC_DIRS 
STATICFILES_DIRS = [ 
    os.path.join(BASE_DIR, 'static'), 
] 

urls.py

from django.conf.urls.static import static 
from django.conf import settings 

urlpatterns = [ 

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

Note

This helper function works only in debug mode and only if the given prefix is local (e.g. /static/) and not a URL (e.g. http://static.example.com/).

Also this helper function only serves the actual STATIC_ROOT folder; it doesn’t perform static files discovery like django.contrib.staticfiles.