【问题标题】:Django App template Loader, can't find app templatesDjango App模板加载器,找不到应用模板
【发布时间】:2014-02-28 09:52:38
【问题描述】:

我的virtualenv 中有以下两个应用程序:backendbackofficebackoffice 应用的模板位于:

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

【问题讨论】:

  • 错误信息中还有什么?不是在找不同的模板吗?
  • @MichalČihař,其他的都是平常的事情......

标签: django templates


【解决方案1】:

啊,我想哭,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',   
)

【讨论】:

    【解决方案2】:

    INSTALLED_APPS 的顺序很重要。但是,当您使用应用程序目录加载器时,防止模板名称冲突的一个好的标准做法是在其目录结构中通过应用程序名称来命名您的模板,例如:

    您的应用程序backoffice 的模板位于:

    backoffice/templates/backoffice/
    

    在实现中,您将渲染模板"backoffice/index.html",它永远不会与django.contrib.admin 中的模板发生冲突。这种设计模式与可插拔应用程序配合得很好。如果您将backoffice 发布为可插拔应用程序,其他用户只需添加backoffice 目录即可覆盖其主模板目录中的模板。

    【讨论】:

      猜你喜欢
      • 2015-04-04
      • 2017-04-20
      • 2018-03-06
      • 1970-01-01
      • 2018-01-02
      • 2012-07-10
      • 2015-03-14
      • 2020-05-27
      相关资源
      最近更新 更多