没有获得django&的静态文件heroku部署

时间:2015-07-05 00:00:38

标签: django heroku gunicorn django-static

我正在开发一个带有Django 1.7的webapp(project_name是" sonata")以及来自"两个Django 1.6"的勺子的项目布局,所以我有一个3层基本文件夹树。

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

在Heroku上部署时遇到问题。我已完成部署,但静态文件未提供给浏览器。

我知道我应该通过设置强制某种方式提供静态文件,然后用google搜索。我已经看过许多方法并阅读有关使用亚马逊服务的内容,但我正在寻找最简单的方法,这将使gUnicorn的未来生产部署变得容易(我希望)。

我尝试修改设置文件(进一步向下,是heroku.py,覆盖base.py)并更改STATIC_ROOT,STATICFILES_DIRS的值并添加以下行:

urlpatterns += static(settings.MEDIA_URL, document_root=settings.STATIC_ROOT)

我一直试着跑步:

heroku run sonata/manage.py collectstatic

之前

git push heroku master

但没有任何反应,甚至用

进行检查
heroku run ls sonata/assets 

正在复制文件。

拜托,我想要一些方向来真正理解我做错了什么并修补它。

在了解heroku和部署时,我遇到了一些使用project_name.wsgi文件的样本ProcFiles,我对此一无所知。

我可以使用一些帮助,因为我阅读的网络越多,我就越困惑。请假设我对部署知之甚少。链接会很有用,但它必须显示新手的材料: - (

非常感谢你提前。

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
# See: https://docs.djangoproject.com/en/1.5/releases/1.5/#allowed-hosts-required-in-production
ALLOWED_HOSTS = ['*']
########## END HOST CONFIGURATION

########## EMAIL CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-host
EMAIL_HOST = environ.get('EMAIL_HOST', 'smtp.gmail.com')

# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-host-password
EMAIL_HOST_PASSWORD = environ.get('EMAIL_HOST_PASSWORD', '')

# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-host-user
EMAIL_HOST_USER = environ.get('EMAIL_HOST_USER', 'your_email@example.com')

# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-port
EMAIL_PORT = environ.get('EMAIL_PORT', 587)

# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-subject-prefix
EMAIL_SUBJECT_PREFIX = '[%s] ' % SITE_NAME

# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-use-tls
EMAIL_USE_TLS = True

# See: https://docs.djangoproject.com/en/dev/ref/settings/#server-email
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
# See: https://docs.djangoproject.com/en/dev/ref/settings/#caches
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
    }
}
########## END CACHE CONFIGURATION


########## SECRET CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
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
# See: https://docs.djangoproject.com/en/dev/ref/settings/#debug
DEBUG = False

# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-debug
TEMPLATE_DEBUG = DEBUG
########## END DEBUG CONFIGURATION


########## MANAGER CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#admins
ADMINS = (
    ('Your Name', 'your_email@example.com'),
)

# See: https://docs.djangoproject.com/en/dev/ref/settings/#managers
MANAGERS = ADMINS
########## END MANAGER CONFIGURATION


########## DATABASE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.',
        'NAME': '',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}
########## END DATABASE CONFIGURATION


########## GENERAL CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#time-zone
TIME_ZONE = 'Europe/Madrid'

# See: https://docs.djangoproject.com/en/dev/ref/settings/#language-code
LANGUAGE_CODE = 'es-es'
DEFAULT_CHARSET = 'utf-8'

# See: https://docs.djangoproject.com/en/dev/ref/settings/#site-id
SITE_ID = 1

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-i18n
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'),
             )

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-l10n
USE_L10N = True

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-tz
USE_TZ = True
########## END GENERAL CONFIGURATION


########## MEDIA CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-root
MEDIA_ROOT = normpath(join(SITE_ROOT, 'media'))

# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-url
MEDIA_URL = '/media/'
########## END MEDIA CONFIGURATION


########## STATIC FILE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = normpath(join(SITE_ROOT, 'assets'))

# 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 = (
    normpath(join(SITE_ROOT, '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',
)
########## 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
# See: https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
# 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
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = []
########## END SITE CONFIGURATION


########## FIXTURE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-FIXTURE_DIRS
FIXTURE_DIRS = (
    normpath(join(SITE_ROOT, 'fixtures')),
)
########## END FIXTURE CONFIGURATION


########## TEMPLATE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors
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.core.context_processors.tz',
    'django.contrib.messages.context_processors.messages',
    'django.core.context_processors.request',
)

# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-loaders
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs
TEMPLATE_DIRS = (
    normpath(join(SITE_ROOT, 'templates')),
    normpath(join(SITE_ROOT, 'templates/registration')),
    normpath(join(SITE_ROOT, 'templates/person')),
)
########## END TEMPLATE CONFIGURATION


########## MIDDLEWARE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#middleware-classes
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
# See: https://docs.djangoproject.com/en/dev/ref/settings/#root-urlconf
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',
)

# See: https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS
########## END APP CONFIGURATION


########## LOGGING CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#logging
# 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.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# 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
# See: https://docs.djangoproject.com/en/dev/ref/settings/#wsgi-application
WSGI_APPLICATION = '%s.wsgi.application' % SITE_NAME

3 个答案:

答案 0 :(得分:2)

我终于明白了。我必须在heroku.py中包含以下行:

<强> heroku.py

# Important for Heroku
BASE_DIR = dirname(dirname(abspath(__file__)))

STATIC_ROOT = 'static' # Important for Heroku
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    path.join(BASE_DIR, 'static'),  # Important for Heroku
)

我必须在wsgi.py中为静态文件添加一些行:

<强> wsgi.py

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

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

并且还更改Procfile:

<强> Procfile

web: gunicorn --chdir sonata --log-file - sonata.wsgi:application

答案 1 :(得分:1)

Procfile告诉Heroku应该启动哪些进程。没有它,Heroku 无法启动应用程序。但首先,对于Python 应用程序需要在存储库的根目录下有一个requirements.txt文件。

我不熟悉 Django的两个独家新闻(我知道这本书,但我已经知道了 从来没有读过它,所以我不确定那个项目布局是什么。它&#39; S 没错,但对我来说似乎过于复杂,特别是对于 总的初学者,因为它偏离了“规范”的铺设方式 一个Django项目。所以如果你刚刚开始,我真的会建议你 与Django一起看看并通过工作 official Django tutorial(该页面上的“第一步”部分)。

现在假设您当前的布局,对于Heroku,您需要在根目录中包含两个文件 您的存储库(位于docsscripts等目录的同一级别 是):Procfilerequirements.txt。假设您的WSGI入口点是 在sonata/sonata目录中(应该称为wsgi.py),Procfile 应该有这样的事情:

web: gunicorn sonata.sonata.wsgi --log-file -

请注意,您在此处展示的Procfile应该能够运行(假设 Python路径是正确的),但您正在使用应该使用的开发服务器 在部署环境中完成。我gunicorn中的Procfile件事 是一个标准的生产质量Python WSGI应用程序服务器和Procfile 简单地说用这个应用程序运行gunicorn

requirements.txt文件至少应该包含:

Django>=1.7
gunicorn

在里面。这个文件告诉Heroku用Pip安装Python包(Python&#39; s 包经理)。在 这种情况是最新版本的Django 1.7分支和Gunicorn。它也应该 列出您的项目具有的其他Python依赖项。此文件的存在 也是告诉Heroku你的应用程序是一个Python应用程序。你应该 真的仔细阅读Getting started with Django on Heroku 指南,因为它涵盖了基础知识和指向进一步阅读。我同意 Heroku的文档对于一个初学者来说有点到处都是, 但是如果你一步一步地使用谷歌来做一些不清楚的事情 对你来说,你应该有一个相对较快的工作设置。

对于静态文件,是的,您可以使用static()函数执行操作 在urlpatterns即使您使用的是Gunicorn而不是开发服务器 但仅限于DEBUG = True中的settings.py,绝对不应该static() 在生产中!如果这只是一个升级或测试,那就没关系,但即便如此 基本的事实仍然存在:Heroku基本上只是一个应用服务器。 它不适用于提供任何静态文件。理论上你可以服务 他们使用static()技巧从Heroku开始,但是只要你进行新的部署 或者一旦应用程序重新启动(可以并且确实经常发生) 由于多种原因,所有的静态文件将消失。所以,实际上, 必须托管其他地方的静态文件,无论是S3还是其他东西。

您在如何设置urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) +\ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 功能时也可能出错 应该(如果你也想提供媒体文件):

collectstatic

同样,这只会在您的应用重新启动之前的短时间内有效 原因,此时您的应用程序已损坏,因为所有静态文件 走了。

另请注意,您无需在Heroku上手动运行collectstatic。它 将检测Django应用程序并在部署时自动运行它。即使 它没有,你在推送存储库之前无法做到这一点。反之亦然 周围,​​你先推,然后{{1}}运行(你会看到它正在运行 自动地)。

在我看来,你真的把头绑在了这个结上,所以我的建议 会退后一步。制作一个非常简单的香草Django应用程序(通过以下 我提到的教程,并尝试将其部署到Heroku,你应该 按照他们链接的Django部署基础知识。即使采用那种方法 是可能的警告,但这些不属于这个问题的范围。一步 一次(尽可能远地回到你想出来的时候),你会没事的。

答案 2 :(得分:1)

  

我正在使用Django 1.7开发一个webapp(project_name是&#34; sonata&#34;)和   来自&#34;两勺Django 1.6&#34;的项目布局,所以我有一个   3层基本文件夹树。

您所指的Two Scoops模板仅适用于1.6,您不应将其用于1.7。

对于django 1.7,你应该使用cookiecutter recipe这是1.7版本的链接;默认存储库是django 1.8。

我不知道为什么你有一个heroku.py文件(这不是来自2scoops的模板的一部分)而且不需要。

2scoops的默认模板假设如下:

  1. 您将使用Heroku部署应用程序代码。
  2. 您将使用AWS S3部署静态文件。
  3. 您将使用Heroku的默认(免费)postgresql服务为您的数据库。
  4. 您将使用sendgrid(再次来自Heroku)发送电子邮件。
  5. 您将使用memcachier(来自Heroku的托管memcache服务)作为缓存。
  6. 确保您希望如何开始,因为您需要单独设置AWS凭据。

    假设您已经从头开始使用django 1.7的正确模板,要在heroku上启动它,您必须确保为Heroku设置适当的环境变量,以确保可以连接适当。执行此操作的步骤列在default readme中(作为项目的一部分生成)。

    请确保follow the guidelines确保您的项目设置正确。

    作为初学者,如果您不熟悉django / heroku / AWS,这可能需要消化很多。

    我建议您按照heroku tutorial for django进行更简单的操作,并立即开始使用。