INSTALLED_APPS =['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',# My apps'learning_logs','users',# Third party apps'bootstrap3',]
# Heroku settings# 这里需要做一定修改,不能用 os.getcwd() == '/app',否则会报错:# django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.if'HOME'in os.environ and os.environ['HOME']=='/app':import dj_database_url
DATABASES ={'default': dj_database_url.config(default='postgres://localhost')}# Honor the 'X-Forwarded-Proto' header for request.is_secure().
SECURE_PROXY_SSL_HEADER =('HTTP_X_FORWARDED_PROTO','https')# Allow only Heroku to host the project
ALLOWED_HOSTS =['plutors.herokuapp.com']# close debug messages
DEBUG =False# Static asset configuration
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT ='staticfiles'
STATICFILES_DIRS =(
os.path.join(PROJECT_DIR,'static'),)
import os
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
os.environ.setdefault('DJANGO_SETTINGS_MODULE','learning.settings')
application = Cling(get_wsgi_application())
在项目目录下,新建static文件夹,并添加placeholder.txt,添加如下内容
This file ensures that learning/static/ will be added to the project.
Django will collect static files and place them in learning/static/.
# 这里要用python3而不是python,否则会报错
heroku run python3 manage.py migrate
优化Heroku部署
创建超级管理员
heroku run bash
python manage.py createsuperuser
exit
创建一个用户友好的URL
# 任意没被使用过且长度大于3的名称都可以
heroku apps:rename xxx
确保实时项目的安全性,打开settings.py,修改如下内容并提交:
# 注意,只修改Heroku部分的配置,这样本地测试仍然可以看到debug信息# Allow only Heroku to host the project
ALLOWED_HOSTS =['plutors.herokuapp.com']# close debug messages,
DEBUG =False