Saturday, July 4, 2015

Re: Not getting static files with django & heroku deployment

Hi Martin,

First let me say unapologetically that I love PyDanny and his work and contributions to the Django community, especially Two Scoops. However, you should know that the philosophy he espouses there is very different from the one Heroku espouses. I've been told he talks about that somewhere, but I've never seen it personally.

I won't go into too much detail, but you can read about it here: https://devcenter.heroku.com/articles/architecting-apps

What you need more than anything is the Heroku Django template :  https://devcenter.heroku.com/articles/django-app-configuration

Try that first. Follow the directions carefully, then let us know if you still need help.


On Saturday, July 4, 2015 at 7:13:39 PM UTC-5, Martin Torre Castro wrote:
Hello,

I'm developing a webapp with Django 1.7 (project_name is "sonata") and the project layout from the "Two scoops of Django 1.6", so I have a 3-tier basic folder tree.

.  ├── requirements  └── sonata      ├── person         └── templatetags      ├── registration      ├── sonata         └── settings      ├── static         ├── css            └── images         ├── fonts         └── js      ├── templates         ├── personApp         └── registrationApp      └── utils          └── templatetags

I have a problem when deploying on Heroku. I have achieved the deployment, but the static files are not being served to the browser.

I know I should force some way of serving the static files through the settings and have googled about. I've seen many ways and read about using Amazon services, but I'm looking for the easiest one, which will make the future production deployment easy as well with gUnicorn (I hope).

I tried modifying the settings file (which is the heroku.py one, which overrides the base.py) and changing values for STATIC_ROOT, STATICFILES_DIRS and adding the line:

    urlpatterns += static(settings.MEDIA_URL, document_root=settings.STATIC_ROOT)
    
I've tried as well running:

    heroku run sonata/manage.py collectstatic
    
before doing 

    git push heroku master

but nothing happens, even checking with 

    heroku run ls sonata/assets 

that the files are being copied.

Please, I would like some orientation for really understanding what I'm doing wrong and mending it. 

When finding out about heroku and deployment I met also some sample ProcFiles which used a project_name.wsgi file and I don't know anything about it.

I could use use some help, because the more webs I read, the more confused I get. Please assume I know very little about deployments. A link would be useful, but it has to show newbie's material :-(

Thank you very much on advance.


    ####################################### ProcFile ##################################
    web: python sonata/manage.py runserver 0.0.0.0:$PORT --noreload




    ####################################### heroku.py ##################################
    # -*- coding: utf-8 -*-
    """Heroku settings and globals."""
    
    from __future__ import absolute_import
    
    from .base import *
    
    from os import environ
    
    # TODO Warning! Heroku retrieve values as strings
    # TODO we should check (only for Heroku) that 'True' and 'False' are respectively True and False 0, 
    # or the equivalent ones, True and False
    def get_env_setting(setting):
        """ Gets the environment variable or an Exception.
        This can be used, for example, for getting the SECRET_KEY and not having it hardcoded in the source code 
        Also for setting the active settings file for local development, heroku, production server, etc... """
        try:
            return environ[setting]
        except KeyError:
            error_msg = "Set the %s env variable" % setting
            raise ImproperlyConfigured(error_msg)
    
    ########## HOST CONFIGURATION
    ALLOWED_HOSTS = ['*']
    ########## END HOST CONFIGURATION
    
    ########## EMAIL CONFIGURATION
    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    
    EMAIL_HOST = environ.get('EMAIL_HOST', 'smtp.gmail.com')
    
    EMAIL_HOST_PASSWORD = environ.get('EMAIL_HOST_PASSWORD', '')
    
    EMAIL_HOST_USER = environ.get('EMAIL_HOST_USER', 'your_...@example.com')
    
    EMAIL_PORT = environ.get('EMAIL_PORT', 587)
    
    EMAIL_SUBJECT_PREFIX = '[%s] ' % SITE_NAME
    
    EMAIL_USE_TLS = True
    
    SERVER_EMAIL = EMAIL_HOST_USER
    ########## END EMAIL CONFIGURATION
    
    ########## DATABASE CONFIGURATION
    import dj_database_url
    DATABASES['default'] = dj_database_url.config()
    # DATABASES = {}
    ########## END DATABASE CONFIGURATION
    
    
    ########## CACHE CONFIGURATION
    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        }
    }
    ########## END CACHE CONFIGURATION
    
    
    ########## SECRET CONFIGURATION
    SECRET_KEY = get_env_setting('SECRET_KEY')
    ########## END SECRET CONFIGURATION




    ####################################### base.py ##################################
    
    # -*- coding: utf-8 -*-
    """Common settings and globals."""
    
    from os.path import abspath, basename, dirname, join, normpath
    from sys import path
    
    
    from os import environ
    
    def get_env_setting(setting):
        """ Gets the environment variable or an Exception.
        This can be used, for example, for getting the SECRET_KEY and not having it hardcoded in the source code 
        Also for setting the active settings file for local development, heroku, production server, etc... """
        try:
            return environ[setting]
        except KeyError:
            error_msg = "Set the %s env variable" % setting
            raise ImproperlyConfigured(error_msg)
    
    
    ########## PATH CONFIGURATION
    # Absolute filesystem path to the Django project directory:
    DJANGO_ROOT = dirname(dirname(abspath(__file__)))
    
    # Absolute filesystem path to the top-level project folder:
    SITE_ROOT = dirname(DJANGO_ROOT)
    
    # Site name:
    SITE_NAME = basename(DJANGO_ROOT)
    
    # Add our project to our pythonpath, this way we don't need to type our project
    # name in our dotted import paths:
    path.append(DJANGO_ROOT)
    ########## END PATH CONFIGURATION
    
    
    ########## DEBUG CONFIGURATION
    DEBUG = False
    
    TEMPLATE_DEBUG = DEBUG
    ########## END DEBUG CONFIGURATION
    
    
    ########## MANAGER CONFIGURATION
    ADMINS = (
        ('Your Name', 'your_...@example.com'),
    )
    
    MANAGERS = ADMINS
    ########## END MANAGER CONFIGURATION
    
    
    ########## DATABASE CONFIGURATION
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.',
            'NAME': '',
            'USER': '',
            'PASSWORD': '',
            'HOST': '',
            'PORT': '',
        }
    }
    ########## END DATABASE CONFIGURATION
    
    
    ########## GENERAL CONFIGURATION
    TIME_ZONE = 'Europe/Madrid'
    
    LANGUAGE_CODE = 'es-es'
    DEFAULT_CHARSET = 'utf-8'
    
    SITE_ID = 1
    
    USE_I18N = True
    
    LOCALE_PATHS = (# Idiomas disponibles en la aplicación
                    SITE_ROOT + '/locale',
                    )
    
    LANGUAGES = (# Ruta donde buscar ficheros de idioma
                     ('es', 'Español'),
                     ('gl', 'Galego'),
                     ('en', 'English'),
                     ('it', 'Italiano'),
                 )
    
    USE_L10N = True
    
    USE_TZ = True
    ########## END GENERAL CONFIGURATION
    
    
    ########## MEDIA CONFIGURATION
    MEDIA_ROOT = normpath(join(SITE_ROOT, 'media'))
    
    MEDIA_URL = '/media/'
    ########## END MEDIA CONFIGURATION
    
    
    ########## STATIC FILE CONFIGURATION
    STATIC_ROOT = normpath(join(SITE_ROOT, 'assets'))
    
    STATIC_URL = '/static/'
    
    STATICFILES_DIRS = (
        normpath(join(SITE_ROOT, 'static')),
    )
    
    STATICFILES_FINDERS = (
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    )
    ########## END STATIC FILE CONFIGURATION
    
    ########## LOGIN REDIRECTION
    # The URL you'd like to redirect users to that aren't logged in
    LOGIN_URL = '/registration/login/'
    #########################################
    
    ########## LOGIN NOT REQUIRED
    # Tuple of regular expressions that lists your exceptions to the default login required on every page.
    LOGIN_EXEMPT_URLS = (
     r'^registration/login\.html$',
     r'^admin/',
    )
    #########################################
    
    ########## SECRET CONFIGURATION
    # Note: This key should only be used for development and testing.
    SECRET_KEY = r"I am not going to show you my secret key, sorry"
    ########## END SECRET CONFIGURATION
    
    
    ########## SITE CONFIGURATION
    # Hosts/domain names that are valid for this site
    ALLOWED_HOSTS = []
    ########## END SITE CONFIGURATION
    
    
    ########## FIXTURE CONFIGURATION
    FIXTURE_DIRS = (
        normpath(join(SITE_ROOT, 'fixtures')),
    )
    ########## END FIXTURE CONFIGURATION
    
    
    ########## TEMPLATE CONFIGURATION
    TEMPLATE_CONTEXT_PROCESSORS = (
        'django.contrib.auth.context_processors.auth',
        'django.core.context_processors.debug',
        'django.core.context_processors.i18n',
        'django.core.context_processors.media',
        'django.core.context_processors.static',
        'django.contrib.messages.context_processors.messages',
        'django.core.context_processors.request',
    )
    
    TEMPLATE_LOADERS = (
        'django.template.loaders.filesystem.Loader',
        'django.template.loaders.app_directories.Loader',
    )
    
    TEMPLATE_DIRS = (
        normpath(join(SITE_ROOT, 'templates')),
        normpath(join(SITE_ROOT, 'templates/registration')),
        normpath(join(SITE_ROOT, 'templates/person')),
    )
    ########## END TEMPLATE CONFIGURATION
    
    
    ########## MIDDLEWARE CONFIGURATION
    MIDDLEWARE_CLASSES = (
        # Default Django middleware.
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.locale.LocaleMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
        # Custom middleware
        'sonata.settings.middleware.LoginRequiredMiddleware',
    )
    ########## END MIDDLEWARE CONFIGURATION
    
    
    ########## URL CONFIGURATION
    ROOT_URLCONF = '%s.urls' % SITE_NAME
    ########## END URL CONFIGURATION
    
    
    ########## APP CONFIGURATION
    DJANGO_APPS = (
        # Default Django apps:
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    
        # Useful template tags:
        # 'django.contrib.humanize',
    
        # Admin panel and documentation:
        'django.contrib.admin',
        # 'django.contrib.admindocs',
    )
    
    # Apps specific for this project go here.
    LOCAL_APPS = (
        'person',
        'registration',
        'utils',
    )
    
    INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS
    ########## END APP CONFIGURATION
    
    
    ########## LOGGING CONFIGURATION
    # A sample logging configuration. The only tangible logging
    # performed by this configuration is to send an email to
    # the site admins on every HTTP 500 error when DEBUG=False.
    # more details on how to customize your logging configuration.
    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'filters': {
            'require_debug_false': {
                '()': 'django.utils.log.RequireDebugFalse'
            }
        },
        'handlers': {
            'mail_admins': {
                'level': 'ERROR',
                'filters': ['require_debug_false'],
                'class': 'django.utils.log.AdminEmailHandler'
            }
        },
        'loggers': {
            'django.request': {
                'handlers': ['mail_admins'],
                'level': 'ERROR',
                'propagate': True,
            },
        }
    }
    ########## END LOGGING CONFIGURATION
    
    
    ########## WSGI CONFIGURATION
    WSGI_APPLICATION = '%s.wsgi.application' % SITE_NAME

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d0639ad4-0403-40bc-b682-9121b625c451%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment