我已经在下面的答案中添加了自己的答案,这已经解决了。
使用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
所以我知道所有静态文件都在正确的目录中。
我的static
和media
文件夹都位于myproject
文件夹下,以及settings.py
和主urls.py
下。
答案 0 :(得分:3)
您必须在设置中将包含静态文件的目录的(绝对)路径添加到STATICFILES_DIRS。你不需要运行collectstatic
答案 1 :(得分: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中提供静态媒体。
答案 2 :(得分:-1)
这是预期的。 Django不提供静态文件。 Heroku(显然)确实如此。
要在本地提供文件,您需要在urls.py
中添加路线以提供文件。