2

我已经在下面的答案中添加了我自己的答案,这已经解决了。

使用 heroku 工具启动服务器时,我可以提供静态文件foreman

$ foreman start
12:25:21 web.1  | started with pid 33574
12:25:21 web.1  | 2013-04-10 12:25:21 [33577] [INFO] Starting gunicorn 0.17.2
12:25:21 web.1  | 2013-04-10 12:25:21 [33577] [INFO] Listening at: http://0.0.0.0:5000 (33577)
12:25:21 web.1  | 2013-04-10 12:25:21 [33577] [INFO] Using worker: sync
12:25:21 web.1  | 2013-04-10 12:25:21 [33582] [INFO] Booting worker with pid: 33582

但它们的静态文件没有使用 runserver 提供:

$ ./manage.py runserver
Validating models...

0 errors found
April 10, 2013 - 12:26:06
Django version 1.5, using settings 'myproject.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

我的Procfile

web: gunicorn myproject.wsgi

我的settings.py(删除了一些设置):

import os
import logging

PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))

DEBUG = True
TEMPLATE_DEBUG = DEBUG

# Using local_settings for this
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': '',
        'PASSWORD': '',
        'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    }
}

# Heroku specific database settings, overridden for local dev in local_settings.py (not in git)
import dj_database_url
DATABASES['default'] =  dj_database_url.config()

# Honor the 'X-Forwarded-Proto' header for request.is_secure() (also needed for Heroku)
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/var/www/example.com/media/"
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = '/media/'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.gzip.GZipMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.middleware.doc.XViewMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'myproject.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'myproject.wsgi.application'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(os.path.dirname(__file__), "templates"),
)

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"
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'django.contrib.admindocs',
    'django.contrib.humanize',
    'django.contrib.flatpages',
    'debug_toolbar',
    'stripe',
    'notification',
    'south',
    'registration',
    'timedelta',
    'sorl.thumbnail',
    'django_messages',
    'tinymce',
)

try:
    from local_settings import *
except ImportError:
    pass

我的主要urls.py

from django.conf.urls import patterns, include, handler500, url
from django.conf import settings
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns(
    '',
    url(r'^$', 'myproject.views.root', name='root'),
    (r'^admin/', include(admin.site.urls)),

)

urlpatterns += patterns('',
    (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
    (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)

正如标题所说,我可以使用foreman start但不能在运行时加载所有静态资产./manage.py runserver

我也跑过./manage.py collectstatic,所以我知道所有静态文件都在正确的目录中。

Mystaticmedia文件夹都位于myproject文件夹下,以及settings.pyurls.py.

4

3 回答 3

3

您必须将包含静态文件的目录的(绝对)路径添加到设置中的 STATICFILES_DIRS。你不需要运行 collectstatic

于 2013-04-10T20:16:46.140 回答
0

我终于让它工作了,以便可以通过 runserver 加载静态文件并部署到 heroku 也可以。

我改变的事情:

settings.py:这里没有改变任何东西,相反,我将覆盖我的 local_settings.py 文件中的一些设置。

STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

local_settings.py: 添加了以下内容:

STATIC_ROOT = ''
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(PROJECT_PATH, 'static'),
)

这允许 heroku 提供您的静态文件以及用于开发的 runserver。值得一提的是,我需要在我的 urls.py 末尾添加额外的静态 url 模式才能在 heroku 中提供静态媒体。

于 2013-04-10T21:14:37.523 回答
-1

这是意料之中的。Django 不提供静态文件。Heroku(显然)确实如此。

要在本地提供文件,您需要添加一个路由urls.py来提供文件。

于 2013-04-10T20:13:02.833 回答