读书人

django 在应用app下边template

发布时间: 2013-10-27 15:21:50 作者: rapoo

django 在应用app下面template

今天做着玩的一个应用。使用django,按照惯例,创建项目后,又创建了一个app,考虑到每个app会有不同的布局,所以在每个app下面新建了一个templates,但是在应用的时候出现了问题。

?

在app下面新建了一个templates的文件夹,然后在这个下面又新建了一个app2的文件夹,在app2里面建了一个index.html 本来我想用他的。 按照惯例,引进各种需要的包以后,开始render

def text_base(request):    return render(request, 'app2/index.html', {'data': 2})

但是出错了。总是说找不到模板。查看了一下堆栈:

TemplateDoesNotExist at /app2/index/下面Template-loader postmortem:Using loader django.template.loaders.app_directories.Loader:/usr/local/lib/python2.7/dist-packages/django/contrib/auth/templates/app2/index.html (File does not exist)

原来他是默认的在django的目录下找,于是乎我把django的template拷贝到我的site目录下,然后设置了一后,出现了新的提示:

TemplateDoesNotExist at /app2/index/Template-loader postmortemDjango tried loading these templates, in this order:Using loader django.template.loaders.filesystem.Loader:/layout/templates/app2/index.html (File does not exist)Using loader django.template.loaders.app_directories.Loader:/usr/local/lib/python2.7/dist-packages/django/contrib/auth/templates/app2/index.html (File does not exist)

?

原来django会在指定的templates下面找,如果找不到的话,再去默认的模板里面找。但是我的问题好像与模板无关。去查了查文档,原来是我们没有install app2 ,在setting.py 里面的INSTALLED_APPS 配置项下加上 ‘app2‘ ?问题就解决了。

?

我的理解是:django会根据安装的app去自己下面templates找,找不到,根据配置去自己设置的目录下找,如果再找不到就去django的templates目录下面找。

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

读书人网 >编程

热点推荐