Django应用程序模板加载程序,找不到应用程序模板 [英] Django App template Loader, can't find app templates

查看:84
本文介绍了Django应用程序模板加载程序,找不到应用程序模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 virtualenv 中有以下两个应用程序:后端 backoffice . backoffice 应用在以下位置具有模板:

I have the following two apps located in my virtualenv: backend backoffice. The backoffice app has templates under:

后台办公/模板

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

So when I login into the Django shell, I can to do the following:

>>>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')也在工作).

So it seems the template is correctly found (l.load_template_source(('index.html') is also working).

但是,当我通过浏览器访问主页时,出现错误:

However, when I access my home page via the browser I get an error:

TemplateDoesNotExist at /

有人可以帮助我解决难题吗?我想念什么?

Can someone help me solve the puzzle? What am I missing?

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

推荐答案

啊,我想哭了,Django怎么会如此棘手...

Ah, I want to cry, how can Django be so tricky ...

我刚刚更新了 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',   
)

这篇关于Django应用程序模板加载程序,找不到应用程序模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆