【问题标题】:How to deal with django Template Does Not exist error in production server?如何处理生产服务器中的django模板不存在错误?
【发布时间】:2013-03-23 15:59:34
【问题描述】:

我刚刚首次将我的应用程序部署到 heroku,但是当我尝试浏览时,我得到了一个 TemplateDoesNotExist 什么困扰着我,当我做python manage.py runserver 时很好。我环顾四周,发现了一些类似的问题,但这些人从未解释过他们是如何解决问题的。

Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/templates/public/homepage.html (File does not exist)

Traceback:
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/app/f4l/live/views.py" in homepage
  156.  return render_to_response('public/homepage.html',context,context_instance=RequestContext(request))
File "/app/.heroku/python/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render_to_response
  20.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  169.         t = get_template(template_name)
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/loader.py" in get_template
  145.     template, origin = find_template(template_name)
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/loader.py" in find_template
  138.     raise TemplateDoesNotExist(name)

Exception Type: TemplateDoesNotExist at /
Exception Value: public/homepage.html

在我的 settings.py 中

PROJECT_ROOT = os.path.join(os.path.dirname(__file__),'..')
SITE_ROOT = PROJECT_ROOT

#FIXTURE_DIRS = (os.path.join(PROJECT_DIR,  '../fixtures'),)
TEMPLATE_DIRS = (os.path.join(PROJECT_ROOT, '../templates'),)
STATICFILES_DIRS = (os.path.join(PROJECT_ROOT, '../static'), os.path.join(PROJECT_ROOT, '../media'), )
STATIC_ROOT = os.path.join(PROJECT_ROOT, '../sitestatic')
MEDIA_ROOT = os.path.join(PROJECT_ROOT, '../media')
MEDIA_URL = "/media/"
STATIC_URL = "/static/"

欢迎阅读任何答案和建议。

【问题讨论】:

  • 这在测试中有效吗?在我看来,您有两个问题,一个是您的 TEMPLATE_DIRS 设置指向项目根目录上方的两个级别,并且您正在尝试从模板根目录中名为模板的目录中获取模板。跨度>
  • 它在开发环境中工作。您如何建议我解决您指定的两个问题。

标签: django heroku django-templates


【解决方案1】:

确保您已遵循 the docs 中的指南。正如 TimD 所指出的,您的静态和模板目录是不寻常的。我更喜欢类似于 DeployDjango.com (found here) 推荐的结构,其中每个应用程序都有自己的静态和模板文件的目录。比如:

|--project_root
   |--apps
      |--my_app
         |--static
         |--templates
   |--manage.py
...

根据文档,collectstatic 命令将自动找到它们:

Put your static files somewhere that staticfiles will find them.
By default, this means within static/ subdirectories of apps in your
INSTALLED_APPS.

还可以通过在静态文件上关注 their brief guide 来检查您是否在 Heroku 上正确运行 collectstatic

【讨论】:

  • 好吧...让我试试这个,这样我就可以接受答案了
猜你喜欢
  • 2012-08-19
  • 2012-05-18
  • 1970-01-01
  • 2019-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-29
  • 1970-01-01
相关资源
最近更新 更多