How to serve django static files on a hero with a gun

I have an application in Django, I deployed it to heroku, but I cannot serve static files on the server, and below is 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 css files (in fact, I am integrated with bootstrap) on the local computer, but after it passed it to the heroku git hub and executed the url like polls.herokuapp.com/polls/ , it It serves html files and all the functionality, but cannot serve the css file.

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

Can someone please let me know what changes need to be made in which files in order to deploy the Django application to a hero?

Notice, I did R&D and applied various codes, for example, using from django.contrib.staticfiles.urls import staticfiles_urlpatterns in urls.py , but could not get a solution.

+4
source share
2 answers

When deploying the application, first change DEBUG=FALSE in the settings file.

The following link may be helpful.

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

+1
source

To serve static files, I highly recommend you 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', # ... ] 

There is no need to run collectstatic as heroku will do it for you. What this does is put all the files in STATIC_ROOT for you (all compressed and with a long hashed file name).

0
source

Source: https://habr.com/ru/post/1489849/


All Articles