2017-09-24 29 views
0

我在學習Django並構建了一個示例Django應用程序。它可以在我的電腦上正常工作,但我在將它部署到Heroku時遇到了問題。 我的根目錄結構如下: First screenDjango中的STATIC_ROOT設置

Second screen

我setttings.py文件中有對靜態文件進行如下設置:

STATIC_URL = '/static/' 
STATICFILES_DIRS = [ 
os.path.join(BASE_DIR, "static") 
] 
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') 
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' 

完整settings.py文件如下:

""" 
Django settings for firstdjango project. 

Generated by 'django-admin startproject' using Django 1.8. 

For more information on this file, see 
https://docs.djangoproject.com/en/1.8/topics/settings/ 

For the full list of settings and their values, see 
https://docs.djangoproject.com/en/1.8/ref/settings/ 
""" 

# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
import os 

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


# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = 'j8s(6fw61+cx_o=g!9a(vs!wbj0&f!7u_lw$([email protected]!b4(' 

# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 

ALLOWED_HOSTS = [] 


# Application definition 

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'inventory', 
) 

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
    'django.middleware.security.SecurityMiddleware', 
) 

ROOT_URLCONF = 'firstdjango.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': ['firstdjango/templates'], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.contrib.messages.context_processors.messages', 
      ], 
     }, 
    }, 
] 

WSGI_APPLICATION = 'firstdjango.wsgi.application' 


# Database 
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', 
     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 


# Internationalization 
# https://docs.djangoproject.com/en/1.8/topics/i18n/ 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 


# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.8/howto/static-files/ 

import dj_database_url 
db_from_env = dj_database_url.config(conn_max_age = 500) 
DATABASES['default'].update(db_from_env) 

# STATIC_URL = '/static/' 
# STATICFILES_DIRS = [ 
# os.path.join(BASE_DIR, "static") 
# ] 
# STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') 

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') 
STATIC_URL = '/static/'  

# Extra places for collectstatic to find static files. 
STATICFILES_DIRS = [ 
    os.path.join(BASE_DIR, 'static'), 
] 
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' 

當我嘗試部署到Heroku時,我得到以下錯誤信息:

ImproperlyConfigured:您正在使用的應用程序staticfiles而不必設置STATIC_ROOT設置一個文件系統路徑

+0

@ user1787331沒關係,但我想知道在Django中設置靜態文件的正確方法以及我所缺少的。 –

+0

我將我的評論移至答案。 – user1787331

+0

請顯示您的其他設置文件。 –

回答

0

下面是靜態根Django的推薦設置 - > Heroku的項目

# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.11/howto/static-files/  

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles') 
STATIC_URL = '/static/'  

# Extra places for collectstatic to find static files. 
STATICFILES_DIRS = [ 
    os.path.join(PROJECT_ROOT, 'static'), 
] 

我會建議只使用這個https://github.com/heroku/heroku-django-template作爲起點,並將您的應用程序導入該項目。由於這樣的事實(截至目前),其Heroku的建議使用下列程序包

Gunicorn 
WhiteNoise 
dj-database-url 

Git項目將爲您提供「靜態檔案,數據庫設置,Gunicorn等生產就緒的配置」換句話說,它會給你正確的配置來將Django部署到Heroku。

相關問題