如何处理生产服务器中的django模板不存在错误?

时间:2013-03-23 15:59:34

标签: django heroku django-templates

我刚刚将我的应用程序部署到了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/"

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

1 个答案:

答案 0 :(得分:0)

确保您已遵循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