我的项目包含三个不同的应用程序。他们使用通用settings.py
:
# -*- coding: utf-8 -*-
import os
SECRET_KEY = '123456'
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DEBUG = True
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'whitenoise.runserver_nostatic',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.humanize',
)
MIDDLEWARE_CLASSES = (
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
INTERNAL_IPS = ('127.0.0.1',)
EMAIL_BACKEND = "sgbackend.SendGridBackend"
SENDGRID_API_KEY = "*******"
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'templates/'),
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.request',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
'DEFAULT_MIDDLEWARE_ALIAS': 'default',
'DEFAULT_MIDDLEWARE_SECONDS': '300',
'DEFAULT_MIDDLEWARE_KEY_PREFIX': '',
}
}
PASSWORD_HASHERS = (
'django.contrib.auth.hashers.SHA1PasswordHasher',
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
你可以看到 - 没什么特别的。每个应用都有其他app#_settings.py
文件,该文件扩展了一般settings.py
。
所有这些应用都在urls.py
:
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
我处于开发模式,DEBUG设置为True。 APP1和APP2能够运行开发服务器,而我第三个APP有问题,过去两天我试图弄清楚问题究竟是什么,但没有成功。当我运行开发服务器(runserver --settings=app3_settings
)时,我收到此错误:
AtributeError: module object has no attribute STATIC_URL
以下是app3_settings.py
:( app1和app2的设置非常相似,几乎没有区别)
# -*- coding: utf-8 -*-
from settings import *
SECRET_KEY = '*************************************************'
ALLOWED_HOSTS = [
'localhost',
'127.0.0.1',
]
INSTALLED_APPS += (
'storages',
'app3',
'el_pagination',
'debug_toolbar',
)
ROOT_URLCONF = 'app3.urls'
WSGI_APPLICATION = 'app3.wsgi.application'
DEFAULT_FROM_EMAIL = 'web@example.com'
MANAGERS = ADMINS
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
'DEFAULT_MIDDLEWARE_ALIAS': 'default',
'DEFAULT_MIDDLEWARE_SECONDS': '300',
'DEFAULT_MIDDLEWARE_KEY_PREFIX': '',
}
}
PASSWORD_HASHERS = (
'django.contrib.auth.hashers.SHA1PasswordHasher',
)
# AMAZON S3 & CLOUDFRONT SERVING MEDIA FILES
AWS_S3_HOST = 's3.eu-central-1.amazonaws.com'
AWS_STORAGE_BUCKET_NAME = 'mybucket'
AWS_CLOUDFRONT_DOMAIN = 'example.cloudfront.net'
AWS_ACCESS_KEY_ID = "*************"
AWS_SECRET_ACCESS_KEY = "************************"
MEDIAFILES_LOCATION = 'app3/media'
MEDIA_ROOT = '/%s/' % MEDIAFILES_LOCATION
MEDIA_URL = '//%s/%s/' % (AWS_CLOUDFRONT_DOMAIN, MEDIAFILES_LOCATION)
DEFAULT_FILE_STORAGE = 'app3.custom_storages.MediaStorage'
# WHITENOISE SERVING STATIC FILES
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'project/static/app3')
STATIC_URL = '/static/'
如您所见,我已正确声明STATIC_URL,STATIC_ROOT,MEDIA_URL和MEDIA_ROOT。我再次强调 - APP1& APP2使用完全相同的概念,但在尝试使用它们运行开发服务器时我没有收到此错误,所以我不明白这一点。
我尝试将app3_settings.py
的这一部分复制到settings.py
:
# AMAZON S3 & CLOUDFRONT SERVING MEDIA FILES
AWS_S3_HOST = 's3.eu-central-1.amazonaws.com'
AWS_STORAGE_BUCKET_NAME = 'mybucket'
AWS_CLOUDFRONT_DOMAIN = 'example.cloudfront.net'
AWS_ACCESS_KEY_ID = "*************"
AWS_SECRET_ACCESS_KEY = "************************"
MEDIAFILES_LOCATION = 'app3/media'
MEDIA_ROOT = '/%s/' % MEDIAFILES_LOCATION
MEDIA_URL = '//%s/%s/' % (AWS_CLOUDFRONT_DOMAIN, MEDIAFILES_LOCATION)
DEFAULT_FILE_STORAGE = 'app3.custom_storages.MediaStorage'
# WHITENOISE SERVING STATIC FILES
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'project/static/app3')
STATIC_URL = '/static/'
然后服务器就可以启动了。但是我不确定为什么我必须在settings.py
和app#_settings.py
中重复这些行,当app1和app2使用相同的设置时它们没有问题。
如果重要,请manage.py
:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
另外,我对每个应用都有单独的wsgi.py
,看起来像这样('app#_settings'是应用之间的唯一区别):
import os
import sys
from whitenoise.django import DjangoWhiteNoise
sys.path.append('/home/user/public_html/project')
os.environ['DJANGO_SETTINGS_MODULE'] = 'app#_settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
application = DjangoWhiteNoise(application)