简体   繁体   English

Django Heroku静态设置

[英]Django Heroku Static Settings

I have a Django app that runs static files fine locally. 我有一个Django应用程序,可以在本地运行静态文件。 When I push to Heroku, it's looking for static files in a different location (home/www_dev/www_dev/settings/static). 当我推送到Heroku时,它正在寻找不同位置的静态文件(home / www_dev / www_dev / settings / static)。

File Structure: 文件结构:

home
> www_dev
>>> organizations (my app)
>>> static
>>> www_dev
>>>>> settings

base.py settings: (used Two Scoops of Django template) base.py设置:(使用两个Django模板的Scoops)

STATIC_ROOT = normpath(join(SITE_ROOT, 'assets'))
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    normpath(join(SITE_ROOT, 'static')),
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

production.py settings (used Heroku documentation) production.py设置 (使用Heroku文档)

# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

wsgi.py wsgi.py

import os
from os.path import abspath, dirname
from sys import path

SITE_ROOT = dirname(dirname(abspath(__file__)))
path.append(SITE_ROOT)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "www_dev.settings.production")

from django.core.wsgi import get_wsgi_application
from dj_static import Cling

application = Cling(get_wsgi_application())

Procfile Procfile

web: gunicorn --pythonpath www_dev www_dev.wsgi -b 0.0.0.0:$PORT

Tried adding to urls.py per other Stack Overflow posts, did not work: 尝试添加到urls.py每个其他Stack Overflow帖子,不起作用:

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

Heroku error: Heroku错误:

-----> Preparing static assets
       Collectstatic configuration error. To debug, run:
       $ heroku run python ./www_dev/manage.py collectstatic --noinput

Result from running collectstatic: 运行collectstatic的结果:

OSError: [Errno 2] No such file or directory: '/app/www_dev/www_dev/settings/static'

I'm willing to just go to S3 if I can't solve this issue, but having trouble finding a good workflow for pushing static (CSS/JS) local to S3 with django-storages and boto. 如果我无法解决这个问题,我很愿意去S3,但是很难找到一个很好的工作流程,用于通过django-storage和boto将静态(CSS / JS)本地推送到S3。 Would be find with having all media on S3. 可以在S3上找到所有媒体。 Any help is much appreciated! 任何帮助深表感谢!

Change the STATIC_ROOT in your settings/production.py 更改settings / production.py中的STATIC_ROOT

If you don't need to use a different static root directory, you can simply delete the variable since it has been defined in the setting/base.py 如果您不需要使用不同的静态根目录,则只需删除该变量,因为它已在设置/ base.py中定义

The heroku doc assumes that you use a single settings.py file which locates in project_dir/settings.py. heroku doc假定您使用位于project_dir / settings.py中的单个settings.py文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM