How to serve django static files on heroku with gunicorn

谁说我不能喝 提交于 2019-12-10 17:41:09

问题


I have an app in Django, I deployed it on heroku but I am unable to serve static files on the server and below are my code and settings:

settings.py

DEBUG = True
TEMPLATE_DEBUG = DEBUG
import os
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
SITE_PATH = os.path.abspath(os.path.join(PROJECT_PATH, os.path.pardir))

STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(SITE_PATH, 'staticfiles'),)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

urls.py

from django.conf import settings
from django.conf.urls import patterns, include, url
from polls.views import VoteClassBasedView
from django.contrib import admin
admin.autodiscover()


urlpatterns = patterns('',
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

Procfile

web: gunicorn django_polls.wsgi

My Local directory structure

drwxrwxr-x 2 user1 user1  4096 Jul  5 11:48 django_polls/
-rwxr-xr-x 1 user1 user1   255 Jun 22 15:50 manage.py*
drwxrwxr-x 5 user1 user1  4096 Jul  3 14:41 polls/
-rw-r--r-- 1 user1 user1 50176 Jul  5 11:41 polls.db
-rw-rw-r-- 1 user1 user1    32 Jul  4 18:57 Procfile
-rw-rw-r-- 1 user1 user1   338 Jun 25 18:51 README.md
-rw-rw-r-- 1 user1 user1   310 Jul  4 17:09 requirements.txt
drwxrwxr-x 5 user1 user1  4096 Jul  4 17:57 staticfiles/
                                                         /bootstrap
                                                         /admin
                                                         /polls 
drwxrwxrwx 3 user1 user1  4096 Jun 22 18:59 templates/

Heroku file structure

drwx------ 2 u19068 19068 4096 2013-07-05 11:31 django_polls/
-rwx------ 1 u19068 19068  255 2013-07-05 11:28 manage.py*
drwx------ 5 u19068 19068 4096 2013-07-05 11:31 polls/
-rw------- 1 u19068 19068   32 2013-07-05 11:28 Procfile
-rw------- 1 u19068 19068  338 2013-07-05 11:28 README.md
-rw------- 1 u19068 19068  310 2013-07-05 11:28 requirements.txt
-rw------- 1 u19068 19068   13 2013-07-05 11:28 runtime.txt
drwx------ 5 u19068 19068 4096 2013-07-05 11:28 staticfiles/
                                                          /bootstrap
                                                          /admin
                                                          /polls 
drwx------ 3 u19068 19068 4096 2013-07-05 11:28 templates/

The above code is able to serve the the css files(Actually I integrated with bootstrap) on local machine, but after committing it to heroku git hub and running the url something like polls.herokuapp.com/polls/ , it is serving the html files and entire functionality but unable to serve the css file.

Also when I run the command foreman start it is also serving the static(css,js) files but the same code when deployed on heroku is unable to server the static files.

Can anyone please let me know what changes needs to be done in what files in order to deploy the Django app on the heroku?

Note, I had done R&D and applied various codes like using from django.contrib.staticfiles.urls import staticfiles_urlpatterns in the urls.py but unable to get the solution.


回答1:


While deploying your app first change DEBUG=FALSE in your setting file.

Following link can be useful.

https://devcenter.heroku.com/articles/django#deploy-to-heroku




回答2:


To serve static files I highly recommend you to use whitenoise http://whitenoise.evans.io/en/stable/django.html

STATIC_ROOT = os.path.join(PROJECT_PATH, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
MIDDLEWARE_CLASSES = [
  # 'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',
  # ...
]

No need to run collectstatic as heroku will do that for you. What that does is puts all the files into the STATIC_ROOT for you (all compressed and with a hashed file name for long expires).



来源:https://stackoverflow.com/questions/17482659/how-to-serve-django-static-files-on-heroku-with-gunicorn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!