【问题标题】:Get TemplateDoesNotExist Error when deploy django application to a pr0duction server将 django 应用程序部署到生产服务器时出现 TemplateDoesNotExist 错误
【发布时间】:2013-03-11 15:26:14
【问题描述】:

环境:

aws 上的 ubuntu
virtalenv 中的 django-1.4.5
带有wsgi的apache-2.2

django setting.py:

TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), '..', 'templates').replace('\\', '/'),)

apache2 错误日志:

[Mon Mar 11 22:18:55 2013] [error] [client 116.238.171.102] mod_wsgi (pid=1794): Exception occurred processing WSGI script '/home/ubuntu/Sites/tushanwan/tushanwan/wsgi.py'.
[Mon Mar 11 22:18:55 2013] [error] [client 116.238.171.102] Traceback (most recent call last):
[Mon Mar 11 22:18:55 2013] [error] [client 116.238.171.102]   File "/home/ubuntu/.virtualenvs/django-1.4.5/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 241, in __call__
[Mon Mar 11 22:18:55 2013] [error] [client 116.238.171.102]   File "/home/ubuntu/.virtualenvs/django-1.4.5/lib/python2.7/site-packages/django/core/handlers/base.py", line 179, in get_response
[Mon Mar 11 22:18:55 2013] [error] [client 116.238.171.102]   File "/home/ubuntu/.virtualenvs/django-1.4.5/lib/python2.7/site-packages/django/core/handlers/base.py", line 228, in handle_uncaught_exception
[Mon Mar 11 22:18:55 2013] [error] [client 116.238.171.102]   File "/home/ubuntu/.virtualenvs/django-1.4.5/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
[Mon Mar 11 22:18:55 2013] [error] [client 116.238.171.102]   File "/home/ubuntu/.virtualenvs/django-1.4.5/lib/python2.7/site-packages/django/views/defaults.py", line 32, in server_error
[Mon Mar 11 22:18:55 2013] [error] [client 116.238.171.102]   File "/home/ubuntu/.virtualenvs/django-1.4.5/lib/python2.7/site-packages/django/template/loader.py", line 145, in get_template
[Mon Mar 11 22:18:55 2013] [error] [client 116.238.171.102]   File "/home/ubuntu/.virtualenvs/django-1.4.5/lib/python2.7/site-packages/django/template/loader.py", line 138, in find_template
[Mon Mar 11 22:18:55 2013] [error] [client 116.238.171.102] TemplateDoesNotExist: 500.html

我花了一天时间,在我的 aws ubuntu 实例上尝试了很多方法来解决这个问题,但它们都不适合我。我使用相同的方式在本地 ubuntu 服务器上部署 django 应用程序,并且可以正常工作。

【问题讨论】:

  • 你设置你的 DEBUG = TRUE 了吗?

标签: django apache deployment


【解决方案1】:

来自:https://docs.djangoproject.com/en/dev/topics/http/views/#the-500-server-error-view

如果 DEBUG 设置为 True(在您的设置模块中),那么您的 500 视图 将永远不会使用,并且将显示回溯, 一些调试信息。

因此,虽然在您的开发服务器中一切正常,但如果在您的生产服务器中您没有 500.html(在模板文件夹的根目录中),那么框架将出现错误。

附注:在 Django 1.4.x 中需要 404.html 和 500.html 文件;在 Django 1.5.x 中,不再严格要求 500.html 和 404.html 文件,谢谢to this patch

【讨论】:

  • 我在模板中添加了 500.html。但仍然在我的 apache 错误日志文件中出现 TemplateDoesNotExist 错误,其中包含我添加的 500.html 的呈现。
  • 你重启了apache吗? 500.html 文件权限可以吗?
  • 是的,apache重启了,500.html的权限是644。
  • TEMPLATE_DIRS 是否被覆盖?打开 django shell 并输入“from django.conf import settings; print settings.TEMPLATE_DIRS”会产生什么结果?
  • 作为一种解决方法,您可以升级到 Django 1.5。它不应该出现这个错误。您也可以暂时将 DEBUG 和 TEMPLATE_DEBUG 设置为 True 以查看是否得到一些提示。最后,我们没有关于异常原因的任何信息......也许作为异常的结果,某些设置变得不可用?
猜你喜欢
  • 2019-11-27
  • 2014-07-08
  • 2021-12-04
  • 2019-12-01
  • 1970-01-01
  • 2020-03-19
  • 2013-07-22
  • 1970-01-01
  • 2020-07-04
相关资源
最近更新 更多