Django App模板Loader,找不到app模板

时间:2014-02-04 12:15:46

标签: django templates

我的virtualenv backend backoffice中有以下两个应用。 backoffice应用的模板位于:

backoffice/templates

因此,当我登录Django shell时,我可以执行以下操作:

>>>from django.template.loaders.app_directories import Loader
>>> list(l.get_template_sources('index.html'))
[u'/var/www/venv2.7/lib/python2.7/site-packages/django/contrib/auth/templates/index.html', u'/var/www/venv2.7/lib/python2.7/site-packages/backoffice/templates/index.html', u'/var/www/venv2.7/lib/python2.7/site-packages/django/contrib/admin/templates/index.html']

所以似乎正确找到了模板(l.load_template_source(('index.html')也正常工作)。

然而,当我通过浏览器访问我的主页时,我收到一个错误:

TemplateDoesNotExist at /

有人可以帮我解决这个难题吗?我错过了什么?

更新:完整引用

Request Method: GET
Request URL: http://192.168.211.140/

Django Version: 1.5.1
Python Version: 2.7.6
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'backoffice',
 'backend',
 'django.contrib.admin')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')

Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/var/www/backoffice/venv2.7/lib/python2.7/site-packages/django/contrib/auth/templates/index.html (File does not exist)
/var/www/backoffice/venv2.7/lib/python2.7/site-packages/django/contrib/admin/templates/index.html (File does not exist)



Traceback:
File "/var/www/backoffice/venv2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/var/www/backoffice/venv2.7/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  25.                 return view_func(request, *args, **kwargs)
File "/var/www/backoffice/venv2.7/lib/python2.7/site-packages/backoffice/views.py" in start_page
  177.     return render(request, 'index.html', context)
File "/var/www/backoffice/venv2.7/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render
  53.     return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/var/www/backoffice/venv2.7/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  170.         t = get_template(template_name)
File "/var/www/backoffice/venv2.7/lib/python2.7/site-packages/django/template/loader.py" in get_template
  146.     template, origin = find_template(template_name)
File "/var/www/backoffice/venv2.7/lib/python2.7/site-packages/django/template/loader.py" in find_template
  139.     raise TemplateDoesNotExist(name)

Exception Type: TemplateDoesNotExist at /
Exception Value: index.html

2 个答案:

答案 0 :(得分:2)

啊,我想哭,Django怎么这么棘手......

我刚刚更新了settings.INSTALLED_APPS

# It was like this
INSTALLED_APPS = ( 

  'django.contrib.auth',
   ...
   'backoffice', 
   'backend',   
   'django.contrib.admin' #!!! NOT ALLOWED HERE
)

# This will workd
INSTALLED_APPS = ( 

  'django.contrib.auth',
   ...
   'django.contrib.admin' # ALLOWED HERE
   'backoffice', 
   'backend',   
)

答案 1 :(得分:1)

INSTALLED_APPS的顺序非常重要。但是,在使用app目录加载器时,防止模板名称冲突的一个很好的标准做法是按照其控制器结构中的应用程序名称命名模板,这样:

您的应用backoffice模板位于:

backoffice/templates/backoffice/

在实施过程中,您将呈现模板"backoffice/index.html",该模板永远不会与django.contrib.admin中的模板发生冲突。这种设计模式与可插拔应用程序相得益彰。如果您将backoffice作为可插拔应用程序发布,其他用户只需添加backoffice目录即可覆盖其主模板目录中的模板。