Django模板在生产服务器中不存在,但工作在:8000

时间:2011-09-29 15:44:23

标签: django django-templates

嗨,大家好我自己的django服务器(在家里)有问题,django无法找到模板,我的错误代码django正在查找正确的位置(/ var / www / project / templates)但没有任何反应< / p>

模板不存在<​​/ strong>但所有路径都没问题。

但使用测试服务器:8000一切都很好,我不知道如何解决这个问题

修改

这非常适合获取模板:

/var/www/agricultura/templates/forms/registro_form.html

模板路径

import os
TEMPLATE_DIRS = (
     os.path.join(os.path.abspath(os.path.dirname(__file__)),'templates'),
)

Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/var/www/agricultura/templates/forms/registro_form.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/usr/local/lib/python2.6/dist-packages/admin_tools/theming/templates/forms/registro_form.html (File does not exist)
/usr/local/lib/python2.6/dist-packages/admin_tools/menu/templates/forms/registro_form.html (File does not exist)
/usr/local/lib/python2.6/dist-packages/admin_tools/dashboard/templates/forms/registro_form.html (File does not exist)
/usr/local/lib/python2.6/dist-packages/django/contrib/admin/templates/forms/registro_form.html (File does not exist)
Using loader django.template.loaders.eggs.Loader:



Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/var/www/agricultura/registro/views.py" in registro_view
  71.                           context_instance=RequestContext(request))
File "/usr/local/lib/python2.6/dist-packages/django/shortcuts/__init__.py" in render_to_response
  20.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader.py" in render_to_string
  181.         t = get_template(template_name)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader.py" in get_template
  157.     template, origin = find_template(template_name)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader.py" in find_template
  138.     raise TemplateDoesNotExist(name)

Exception Type: TemplateDoesNotExist at /registro/
Exception Value: forms/registro_form.html

有什么想法吗?

感谢

3 个答案:

答案 0 :(得分:0)

模板中可能存在错误。你在设置中有TEMPLATE_DEBUG = True吗?你也设置了TEMPLATE_DIRS?

尝试通过服务器上的python shell加载模板:

https://docs.djangoproject.com/en/dev/ref/templates/api/#the-render-to-string-shortcut

./manage.py shell
from django.template.loader import render_to_string
rendered = render_to_string('my_template.html', { 'foo': 'bar' })

它可能会抛出错误并给你一个进一步的线索。

答案 1 :(得分:0)

将其添加到您的设置文件中:

def findMyWay(*relativeComponents):
    return os.path.join(os.path.dirname(__file__), *relativeComponents).replace("\\","/")

TEMPLATE_LOADERS = (
    "django.template.loaders.filesystem.Loader",
    "django.template.loaders.app_directories.Loader",
)

TEMPLATE_DIRS = (
    findMyWay("templates"),
)

对我来说工作得很好。

答案 2 :(得分:0)

琐碎,但我看到人们忘记在已安装的应用中添加相关应用程序并获取模板未找到错误的情况。