2017-02-22 133 views
0

我知道有类似的问题,但相信我已经经历了每一个,但仍然没有让我的静态文件通过。 任何帮助,不胜感激。Heroku没有找到静态文件Django

我有我所有的设置和下面的其他代码。

下面的截图显示了,当我运行
$ Heroku的执行python manage.py collectstatic
和erorr当我部署我的应用程序,我得到。

Capture

setting.py:

import os 
import dj_database_url 

# this is because I have my setting.py under setting folder 
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 

SECRET_KEY = os.environ['secret_key'] 

DEBUG = False 

ADMINS = [] 
ALLOWED_HOSTS = ['*'] 

EMAIL_HOST = 'smtp.gmail.com' 
EMAIL_HOST_USER = '[email protected]' 
EMAIL_HOST_PASSWORD = os.environ['EMAIL_HOST_PASSWORD'] 
EMAIL_PORT = 587 
EMAIL_USE_TLS = True 

INSTALLED_APPS = [ 
    'django.contrib.admin', 
    'django.contrib.sites', 
    'registration', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    # third party apps 
    'crispy_forms', 
    # my apps 
    'home', 
] 

MIDDLEWARE = [ 
    'django.middleware.security.SecurityMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
] 

ROOT_URLCONF = 'site2.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [os.path.join(BASE_DIR, 'templates')], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.template.context_processors.i18n', 
       'django.template.context_processors.media', 
       'django.template.context_processors.static', 
       'django.template.context_processors.tz', 
       'django.contrib.messages.context_processors.messages', 
      ], 
     }, 
    }, 
] 

WSGI_APPLICATION = 'site2.wsgi.application' 


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

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

AUTH_PASSWORD_VALIDATORS = [ 
    { 
     'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 
    }, 
] 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 


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


STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' 

# registration settings 
ACCOUNT_ACTIVATION_DAYS = 7 
REGISTRATION_AUTO_LOGIN = True 
SITE_ID = 1 
LOGIN_REDIRECT_URL = '/' 

# crispy form settings 
CRISPY_TEMPLATE_PACK = 'bootstrap3' 

Procfile:

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

wsgi.py:

import os 

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

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "site2.settings") 

application = get_wsgi_application() 
application = DjangoWhiteNoise(application) 

urls.py:

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

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^accounts/', include('registration.backends.default.urls')), 
    url(r'^', include('home.urls')), 
] 

if not settings.DEBUG: 
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

这是一个不正确加载我的模板的一部分:

<!DOCTYPE html> 
{% load static from staticfiles %} 
<html lang="en"> 
    <head> 

    <!-- Custom styles for this template --> 
    <link href="{% static 'css/offcanvas.css' %}" rel="stylesheet"> 
</head> 
+0

尝试{%load static%}而不是{%static static from staticfiles%} – Darshan

+0

不成功! – Majid

回答

0

变化urls.py:

if settings.DEBUG is True: 
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

或者删除静态URL中的服务代码,因为whitenose是服务静态文件,你不需要从heroku中的django提供静态文件。

+0

仍然无法使用!我注释了URL中的静态服务代码,并将每一个再次推送到heroku,我看到它处理静态文件时,我做$ heroku运行python manage.py collectstatic [后处理的'js/googlemap.js'为'js/googlemap.fbc11c53299d .js'],但它仍然无法在我的应用中找到.js或.css – Majid

+0

请将'STATICFILES_DIRS = [os.path.join(BASE_DIR,'static')]'改为'STATICFILES_DIRS = [os.path.join BASE_DIR,'static'),]' – ruddra

+0

不!还是行不通! – Majid