我正在Heroku上的生产环境中部署Django Web应用程序。使用heroku logs --tail
检查后,收到服务器错误(500)。
我不确定如何消除可能引起问题的用户警告。
注意,我是从prod.py文件中运行应用程序的,而我的wsgi.py设置为:
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings.prod") # mysite.settings.prod when in prod
application = get_wsgi_application()
requirements.txt
dj-database-url==0.5.0
Django==2.1.2
django-heroku==0.3.1
# psycopg2==2.7.5
psycopg2-binary==2.7.5
pytz==2018.6
whitenoise==4.1
gunicorn==19.6.0
编辑
Heroku CLI中的日志文件
我已经在本地开发人员和生产人员之间调整了Heroku PostgresQL版本,并在本地开发人员中添加了PostgresQL以使其更加一致。当我测试DEBUG = True时,该站点正常工作。我在Chrome控制台日志中找不到任何错误
当前的Heroku日志为:
2018-10-27T18:23:12.548825+00:00 app[web.1]: 10.170.101.130 - - [27/Oct/2018:18:23:12 +0000] "GET /static/assets/js/util.js HTTP/1.1" 304 0 "https://appname etc etc/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
2018-10-27T18:23:12.551799+00:00 app[web.1]: 10.67.229.158 - - [27/Oct/2018:18:23:12 +0000] "GET /static/assets/js/main.js HTTP/1.1" 304 0 "https://appname etc etc/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
2018-10-27T18:23:12.557365+00:00 app[web.1]: 10.182.99.110 - - [27/Oct/2018:18:23:12 +0000] "GET /static/assets/img/prolay_ortho_view_logo.jpg HTTP/1.1" 304 0 "https://appname etc etc/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
2018-10-27T18:23:12.560874+00:00 app[web.1]: 10.43.196.72 - - [27/Oct/2018:18:23:12 +0000] "GET /static/assets/img/pexels-photo-574071.jpeg HTTP/1.1" 304 0 "https://appname etc etc/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
2018-10-27T18:23:12.562847+00:00 app[web.1]: 10.168.66.175 - - [27/Oct/2018:18:23:12 +0000] "GET /static/assets/img/safe.JPG HTTP/1.1" 304 0 "https://appname etc etc/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
2018-10-27T18:23:12.581753+00:00 app[web.1]: 10.67.229.158 - - [27/Oct/2018:18:23:12 +0000] "GET /static/assets/img/Pipeline%20codes_w1134xh342pxl_tcm8-6122.jpg HTTP/1.1" 304 0 "https://appname etc etc/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
2018-10-27T18:23:12.582343+00:00 app[web.1]: 10.182.99.110 - - [27/Oct/2018:18:23:12 +0000] "GET /static/assets/img/3l-image-30.jpg HTTP/1.1" 304 0 "https://appname etc etc/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
2018-10-27T18:23:12.591523+00:00 app[web.1]: 10.168.66.175 - - [27/Oct/2018:18:23:12 +0000] "GET /static/assets/img/csm_Aegir_birdeye_d49a16a969.jpg HTTP/1.1" 304 0 "https://appname etc etc/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
2018-10-27T18:23:12.598203+00:00 app[web.1]: 10.170.101.130 - - [27/Oct/2018:18:23:12 +0000] "GET /static/assets/img/tumblr_inline_mz6qte2Eu41r1ki0v.png HTTP/1.1" 304 0 "https://appname etc etc/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
2018-10-27T18:23:12.599010+00:00 app[web.1]: 10.43.196.72 - - [27/Oct/2018:18:23:12 +0000] "GET /static/assets/img/FSHR.png HTTP/1.1" 304 0 "https://appname etc etc/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
2018-10-27T18:23:12.623129+00:00 app[web.1]: 10.182.99.110 - - [27/Oct/2018:18:23:12 +0000] "GET /static/assets/img/download.jpg HTTP/1.1" 304 0 "https://appname etc etc/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
2018-10-27T18:23:12.658237+00:00 app[web.1]: 10.164.78.142 - - [27/Oct/2018:18:23:12 +0000] "GET /static/assets/img/pexels-photo-546819.jpeg HTTP/1.1" 304 0 "https://appname etc etc/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
2018-10-27T18:23:12.557851+00:00 heroku[router]: at=info method=GET path="/static/assets/img/prolay_ortho_view_logo.jpg" host=appname etc etc request_id=6b463fc0-d4b3-41b0-953c-31bcde664c0b fwd="68.129.236.143" dyno=web.1 connect=0ms service=3ms status=304 bytes=168 protocol=https
2018-10-27T18:23:12.623553+00:00 heroku[router]: at=info method=GET path="/static/assets/img/download.jpg" host=appname etc etc request_id=d50864eb-3f17-4bea-99d2-d22e3ca20134 fwd="68.129.236.143" dyno=web.1 connect=0ms service=3ms status=304 bytes=167 protocol=https
2018-10-27T18:23:12.591261+00:00 heroku[router]: at=info method=GET path="/static/assets/img/csm_Aegir_birdeye_d49a16a969.jpg" host=appname etc etc request_id=74a1e2e3-af5b-4433-bd18-0e43df9e2673 fwd="68.129.236.143" dyno=web.1 connect=0ms service=5ms status=304 bytes=168 protocol=https
2018-10-27T18:23:12.524893+00:00 heroku[router]: at=info method=GET path="/static/assets/js/jquery.scrolly.min.js" host=appname etc etc request_id=28eed4be-a225-4061-a944-c1f8d9dcda94 fwd="68.129.236.143" dyno=web.1 connect=1ms service=3ms status=304 bytes=166 protocol=https
2018-10-27T18:23:12.595552+00:00 heroku[router]: at=info method=GET path="/static/assets/img/FSHR.png" host=appname etc etc request_id=51a12235-6cc0-433d-b73c-860c3ab73b8d fwd="68.129.236.143" dyno=web.1 connect=1ms service=12ms status=304 bytes=168 protocol=https
2018-10-27T18:23:12.531496+00:00 heroku[router]: at=info method=GET path="/static/assets/js/browser.min.js" host=appname etc etc request_id=dc5b8f1b-dd1b-4b27-9c6f-aa045bd4f940 fwd="68.129.236.143" dyno=web.1 connect=1ms service=6ms status=304 bytes=166 protocol=https
2018-10-27T18:23:12.563021+00:00 heroku[router]: at=info method=GET path="/static/assets/img/safe.JPG" host=appname etc etc request_id=fa116a25-c1fc-41c6-812f-e2704a7a875d fwd="68.129.236.143" dyno=web.1 connect=0ms service=8ms status=304 bytes=167 protocol=https
2018-10-27T18:23:12.583046+00:00 heroku[router]: at=info method=GET path="/static/assets/img/3l-image-30.jpg" host=appname etc etc request_id=63539c68-52ed-42a7-b563-db8ec9d359bd fwd="68.129.236.143" dyno=web.1 connect=0ms service=3ms status=304 bytes=167 protocol=https
2018-10-27T18:23:12.598417+00:00 heroku[router]: at=info method=GET path="/static/assets/img/tumblr_inline_mz6qte2Eu41r1ki0v.png" host=appname etc etc request_id=b21b3161-ed8c-4ab1-a414-61fcb220a8b0 fwd="68.129.236.143" dyno=web.1 connect=0ms service=8ms status=304 bytes=168 protocol=https
2018-10-27T18:23:12.773676+00:00 app[web.1]: 10.164.78.142 - - [27/Oct/2018:18:23:12 +0000] "GET /static/assets/fonts/fontawesome-webfont.woff2?v=4.7.0 HTTP/1.1" 304 0 "https://appname etc etc/static/assets/css/font-awesome.min.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
2018-10-27T18:23:12.431862+00:00 heroku[router]: at=info method=GET path="/static/assets/js/jquery.min.js" host=appname etc etc request_id=bb6f4c97-2d36-4a66-a346-94f86fef7990 fwd="68.129.236.143" dyno=web.1 connect=0ms service=2ms status=304 bytes=168 protocol=https
2018-10-27T18:23:12.526196+00:00 heroku[router]: at=info method=GET path="/static/assets/js/breakpoints.min.js" host=appname etc etc request_id=2e6df0b3-93d6-470d-8791-dcc410f4d50b fwd="68.129.236.143" dyno=web.1 connect=0ms service=2ms status=304 bytes=166 protocol=https
2018-10-27T18:23:12.667031+00:00 heroku[router]: at=info method=GET path="/static/assets/img/pexels-photo-546819.jpeg" host=appname etc etc request_id=7dce7dbc-646d-4bb3-8c98-c9b0355c3bf8 fwd="68.129.236.143" dyno=web.1 connect=6ms service=13ms status=304 bytes=167 protocol=https
2018-10-27T18:23:12.551812+00:00 heroku[router]: at=info method=GET path="/static/assets/js/main.js" host=appname etc etc request_id=36332bfd-ce76-4724-bc84-4461db44f0c9 fwd="68.129.236.143" dyno=web.1 connect=0ms service=2ms status=304 bytes=166 protocol=https
2018-10-27T18:23:12.582055+00:00 heroku[router]: at=info method=GET path="/static/assets/img/Pipeline%20codes_w1134xh342pxl_tcm8-6122.jpg" host=appname etc etc request_id=6b91e488-58b2-48ab-977f-803347340c8d fwd="68.129.236.143" dyno=web.1 connect=0ms service=3ms status=304 bytes=168 protocol=https
2018-10-27T18:23:12.775494+00:00 heroku[router]: at=info method=GET path= "/static/assets/fonts/fontawesome-webfont.woff2?v=4.7.0" host=appname etc etc request_id=bb89dfef-08a5-4f2f-9a0c-be7794da3be2 fwd="68.129.236.143" dyno=web.1 connect=4ms service=14ms status=304 bytes=168 protocol=https
2018-10-27T18:23:21.894052+00:00 heroku[router]: at=info method=GET path="/static/assets/css/font-awesome.min.css" host=appname etc etc request_id=f3ae4cf5-2d66-4747-b7ef-e8eb8ca29381 fwd="68.129.236.143" dyno=web.1 connect=0ms service=15ms status=304 bytes=167 protocol=https
2018-10-27T18:23:21.893614+00:00 app[web.1]: 10.61.174.77 - - [27/Oct/2018:18:23:21 +0000] "GET /static/assets/css/font-awesome.min.css HTTP/1.1" 304 0 "https://appname etc etc/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
2018-10-27T18:23:21.894116+00:00 app[web.1]: 10.113.170.136 - - [27/Oct/2018:18:23:21 +0000] "GET /static/assets/css/main.css HTTP/1.1" 304 0 "https://appname etc etc/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
2018-10-27T18:23:21.882264+00:00 heroku[router]: at=info method=GET path="/static/assets/css/main.css" host=appname etc etc request_id=7e6fd1d8-5500-4636-a3a2-fe59fea60de4 fwd="68.129.236.143" dyno=web.1 connect=0ms service=7ms status=304 bytes=167 protocol=https
2018-10-27T18:39:16.000000+00:00 app[api]: Build started by user xxx@xxxx.com
2018-10-27T18:39:43.384267+00:00 app[api]: Deploy 5a9f1f4e by user xxx@xxxx.com
2018-10-27T18:39:43.384267+00:00 app[api]: Release v37 created by user xxx@xxxx.com
2018-10-27T18:39:43.768341+00:00 heroku[web.1]: Restarting
2018-10-27T18:39:43.769068+00:00 heroku[web.1]: State changed from up to starting
2018-10-27T18:39:44.595326+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2018-10-27T18:39:44.617853+00:00 app[web.1]: [2018-10-27 18:39:44 +0000] [10] [INFO] Worker exiting (pid: 10)
2018-10-27T18:39:44.626110+00:00 app[web.1]: [2018-10-27 18:39:44 +0000] [11] [INFO] Worker exiting (pid: 11)
2018-10-27T18:39:44.675226+00:00 app[web.1]: [2018-10-27 18:39:44 +0000] [4] [INFO] Handling signal: term
2018-10-27T18:39:44.776404+00:00 app[web.1]: [2018-10-27 18:39:44 +0000] [4] [INFO] Shutting down: Master
2018-10-27T18:39:44.944076+00:00 heroku[web.1]: Process exited with status 0
2018-10-27T18:39:50.467127+00:00 heroku[web.1]: Starting process with command `gunicorn mysite.wsgi --log-file -`
2018-10-27T18:39:54.000000+00:00 app[api]: Build succeeded
2018-10-27T18:39:54.293554+00:00 app[web.1]: [2018-10-27 18:39:54 +0000] [4] [INFO] Starting gunicorn 19.9.0
2018-10-27T18:39:54.295231+00:00 app[web.1]: [2018-10-27 18:39:54 +0000] [4] [INFO] Listening at: http://0.0.0.0:5467 (4)
2018-10-27T18:39:54.295432+00:00 app[web.1]: [2018-10-27 18:39:54 +0000] [4] [INFO] Using worker: sync
2018-10-27T18:39:54.302585+00:00 app[web.1]: [2018-10-27 18:39:54 +0000] [10] [INFO] Booting worker with pid: 10
2018-10-27T18:39:54.405429+00:00 app[web.1]: [2018-10-27 18:39:54 +0000] [11] [INFO] Booting worker with pid: 11
2018-10-27T18:39:54.878799+00:00 heroku[web.1]: State changed from starting to up
2018-10-27T18:39:56.637508+00:00 app[web.1]: /app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
2018-10-27T18:39:56.637526+00:00 app[web.1]: """)
2018-10-27T18:39:56.640843+00:00 app[web.1]: /app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
2018-10-27T18:39:56.640847+00:00 app[web.1]: """)
2018-10-27T18:40:00.946078+00:00 heroku[router]: at=info method=GET path="/" host=appname etc etc request_id=67a28d3f-33cf-4de5-aae2-46e28dd4e0c5 fwd="68.129.236.143" dyno=web.1 connect=1ms service=663ms status=500 bytes=361 protocol=https
2018-10-27T18:40:00.947531+00:00 app[web.1]: 10.101.183.181 - - [27/Oct/2018:18:40:00 +0000] "GET / HTTP/1.1" 500 27 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
2018-10-27T18:41:20.436823+00:00 app[api]: Starting process with command `python manage.py migrate` by user xxx@xxxx.com
2018-10-27T18:41:28.736259+00:00 heroku[run.4727]: Awaiting client
2018-10-27T18:41:28.792885+00:00 heroku[run.4727]: Starting process with command `python manage.py migrate`
2018-10-27T18:41:28.823836+00:00 heroku[run.4727]: State changed from starting to up
2018-10-27T18:41:36.996317+00:00 heroku[run.4727]: Process exited with status 0
2018-10-27T18:41:37.014215+00:00 heroku[run.4727]: State changed from up to complete
2018-10-27T18:41:54.908245+00:00 app[api]: Starting process with command `python manage.py check --deploy` by user xxx@xxxx.com
2018-10-27T18:42:04.214344+00:00 heroku[run.7761]: Awaiting client
2018-10-27T18:42:04.276622+00:00 heroku[run.7761]: Starting process with command `python manage.py check --deploy`
2018-10-27T18:42:04.288978+00:00 heroku[run.7761]: State changed from starting to up
2018-10-27T18:42:11.963509+00:00 heroku[run.7761]: Process exited with status 0
2018-10-27T18:42:11.981542+00:00 heroku[run.7761]: State changed from up to complete
2018-10-27T18:42:33.339983+00:00 app[api]: Starting process with command `pip install psycopg2-binary` by user xxx@xxxx.com
2018-10-27T18:42:38.522056+00:00 heroku[run.7464]: Awaiting client
2018-10-27T18:42:38.545692+00:00 heroku[run.7464]: Starting process with command `pip install psycopg2-binary`
2018-10-27T18:42:38.827435+00:00 heroku[run.7464]: State changed from starting to up
2018-10-27T18:42:44.485848+00:00 heroku[run.7464]: State changed from up to complete
2018-10-27T18:42:44.471458+00:00 heroku[run.7464]: Process exited with status 0
2018-10-27T18:47:04.000000+00:00 app[api]: Build started by user xxx@xxxx.com
2018-10-27T18:47:28.096216+00:00 app[api]: Deploy de1bada0 by user xxx@xxxx.com
2018-10-27T18:47:28.096216+00:00 app[api]: Release v38 created by user xxx@xxxx.com
2018-10-27T18:47:29.176560+00:00 heroku[web.1]: Restarting
2018-10-27T18:47:29.191742+00:00 heroku[web.1]: State changed from up to starting
2018-10-27T18:47:30.020966+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2018-10-27T18:47:30.039591+00:00 app[web.1]: [2018-10-27 18:47:30 +0000] [10] [INFO] Worker exiting (pid: 10)
2018-10-27T18:47:30.040480+00:00 app[web.1]: [2018-10-27 18:47:30 +0000] [11] [INFO] Worker exiting (pid: 11)
2018-10-27T18:47:30.063690+00:00 app[web.1]: [2018-10-27 18:47:30 +0000] [4] [INFO] Handling signal: term
2018-10-27T18:47:30.165169+00:00 app[web.1]: [2018-10-27 18:47:30 +0000] [4] [INFO] Shutting down: Master
2018-10-27T18:47:30.262607+00:00 heroku[web.1]: Process exited with status 0
2018-10-27T18:47:36.983340+00:00 heroku[web.1]: Starting process with command `gunicorn mysite.wsgi --log-file -`
2018-10-27T18:47:39.000000+00:00 app[api]: Build succeeded
2018-10-27T18:47:40.793859+00:00 heroku[web.1]: State changed from starting to up
2018-10-27T18:47:40.567667+00:00 app[web.1]: [2018-10-27 18:47:40 +0000] [4] [INFO] Starting gunicorn 19.9.0
2018-10-27T18:47:40.568549+00:00 app[web.1]: [2018-10-27 18:47:40 +0000] [4] [INFO] Listening at: http://0.0.0.0:59727 (4)
2018-10-27T18:47:40.568707+00:00 app[web.1]: [2018-10-27 18:47:40 +0000] [4] [INFO] Using worker: sync
2018-10-27T18:47:40.574950+00:00 app[web.1]: [2018-10-27 18:47:40 +0000] [10] [INFO] Booting worker with pid: 10
2018-10-27T18:47:40.657436+00:00 app[web.1]: [2018-10-27 18:47:40 +0000] [11] [INFO] Booting worker with pid: 11
2018-10-27T18:47:42.134059+00:00 app[web.1]: /app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
2018-10-27T18:47:42.134082+00:00 app[web.1]: """)
2018-10-27T18:47:42.166420+00:00 app[web.1]: /app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
2018-10-27T18:47:42.166424+00:00 app[web.1]: """)
2018-10-27T18:48:30.044844+00:00 heroku[router]: at=info method=GET path="/" host=appname etc etc request_id=3fc78864-ef33-4259-8ed2-69dbfdb64ec7 fwd="68.129.236.143" dyno=web.1 connect=0ms service=508ms status=500 bytes=361 protocol=https
2018-10-27T18:48:30.043986+00:00 app[web.1]: 10.111.224.2 - - [27/Oct/2018:18:48:30 +0000] "GET / HTTP/1.1" 500 27 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
prod.py
from mysite.settings.base import *
# Overide base settings
DEBUG = False # False in Prod
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
'debug': DEBUG,
},
},
]
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
# }
DATABASES = {
'default': {
'ENGINE': os.environ['ENGINE'],
'NAME': os.environ['NAME'],
'USER': os.environ['USER'],
'PASSWORD': os.environ['PASSWORD'],
'HOST': os.environ['HOST'],
'PORT': os.environ['PORT'],
}
}
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
# DATABASE_URL = os.environ['DATABASE_URL']
# Change 'default' database configuration with $DATABASE_URL.
# DATABASES['default'].update(dj_database_url.config(conn_max_age=500, ssl_require=True))
# DATABASES['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True)
# Allow all host headers
ALLOWED_HOSTS = ['*']
SECRET_KEY = os.environ['SECRET_KEY']
EMAIL_BACKEND = os.environ['EMAIL_BACKEND']
EMAIL_HOST = os.environ['EMAIL_HOST']
EMAIL_USE_TLS = os.environ['EMAIL_USE_TLS']
EMAIL_PORT = os.environ['EMAIL_PORT']
EMAIL_HOST_USER = os.environ['EMAIL_HOST_USER']
EMAIL_HOST_PASSWORD = os.environ['EMAIL_HOST_PASSWORD']
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_BROWSER_XSS_FILTER = True
SECURE_SSL_REDIRECT = True
X_FRAME_OPTIONS = 'DENY'
SECURE_HSTS_SECONDS = 60
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
# Heroku: Update database configuration from $DATABASE_URL.
import dj_database_url
DATABASES['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True)
try:
from mysite.settings.local import *
except:
pass
答案 0 :(得分:0)
我找到了解决方法。
第1步
将其添加到生产设置文件(this code is from here的源):
# Logging
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG'),
},
},
}
第2步
确保您在本地运行python manage.py collectstatic --noinput
,并使用git push heroku master
将代码推送到Heroku服务器。也可以使用heroku run python manage.py collectstatic --noinput
第3步
使用heroku logs --tail
捕获任何错误,这些错误将通过在步骤1中调用代码来突出显示。对于我来说,我有:
ValueError: Missing staticfiles manifest entry for 'assets/css/main.css
'
此外,我使用以下代码消除了有关psycog二进制库的警告。
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
和import psycopg2
在我的基本设置文件中。
最后,其他地方对Whitenoise和静态文件目录进行了一些讨论。
我使用了以下设置:
import os
import dj_database_url
import django_heroku
import psycopg2
# import sys
# from secrets import *
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
# Application definition
INSTALLED_APPS = [
# Disable Django's own staticfiles handling in favour of WhiteNoise, for
# greater consistency between gunicorn and `./manage.py runserver`. See:
# http://whitenoise.evans.io/en/stable/django.html#using-whitenoise-in-development
'whitenoise.runserver_nostatic',
'django.contrib.staticfiles',
'landing',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'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 = 'mysite.urls'
WSGI_APPLICATION = 'mysite.wsgi.application'
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',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
ADMINS = (('Webmaster','peterdoh@gmail.com'))
MANAGERS = ADMINS
# Change 'default' database configuration with $DATABASE_URL.
# DATABASES['default'].update(dj_database_url.config(conn_max_age=500, ssl_require=True))
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/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'),
# ]
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
# Activate Django-Heroku.
django_heroku.settings(locals())
最后,在 wsgi.py 文件中,确保此配置:
import os
from django.core.wsgi import get_wsgi_application
# Whitenoise should not appear here: http://whitenoise.evans.io/en/stable/changelog.html#v4-0
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings.prod") # mysite.settings.prod when in prod
application = get_wsgi_application()