简体   繁体   English

Django静态文件和Heroku

[英]Django static files and Heroku

In my main project directory I have a settings directory which has the following files: local.py , base.py , production.py and __init__.py . 在我的主项目目录中,我有一个设置目录,其中包含以下文件: local.pybase.pyproduction.py__init__.py On running collectstatic files are saved in the folder staticfiles in the project directory 在运行时,collectstatic文件保存在项目目录中的staticfiles文件夹中

local.py

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

PROJECT_DIR  = os.path.dirname(__file__) 

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

MEDIA_URL = '/media/'

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

STATIC_URL = '/static/'

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

base.py

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)


STATIC_URL = '/static/'

PROJECT_DIR  = os.path.dirname(__file__) 

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

MEDIA_URL = '/media/'

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

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

production.py

import os
from django.conf import settings


DEBUG = False
TEMPLATE_DEBUG = True

# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES['default'] =  dj_database_url.config()

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers
ALLOWED_HOSTS = ['*']


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

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

When I run the following command: heroku run python manage.py collectstatic --noinput 当我运行以下命令时: heroku run python manage.py collectstatic --noinput

I am getting the error: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path. 我收到错误消息: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.

A good template / guide for deploying on Heroku is here . 对于在Heroku上部署一个很好的模板/指南是在这里

Take a look at their settings.py torwards the end:: 看一下它们的settings.py到底是什么:

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

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

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

Don't use a relative path in the STATIC_ROOT in your production.py file. 不要在production.py文件的STATIC_ROOT中使用相对路径。

Use it like your do in your base.py 像在base.py中一样使用它

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

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

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