模板位置
djproject/templates/jobs
|-- base.html
|-- job_1list.html
`-- job_detail.html当前urls.py
urlpatterns = patterns('',
url(r'^$',
ListView.as_view(
queryset=Job.objects.order_by('-pub_date')[:3],
context_object_name='job_list',
template_name='jobs/job_2list.html')),
url(r'^(?P<pk>\d+)/$',
DetailView.as_view(
model=Job,
template_name='tjobs/job_detail.html')),
# url(r'^admin/', admin.site.urls),
url(r'^admin/', admin.site.urls),
)访问127.0.0.1:8000,访问的是job_list.html,出错
TemplateDoesNotExist at /
jobs/job_2list.html, jobs/job_list.html
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/home/python/djproject/templates/jobs/job_2list.html, jobs/job_list.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/usr/lib/python2.6/site-packages/django/contrib/auth/templates/jobs/job_2list.html, jobs/job_list.html (File does not exist)
/usr/lib/python2.6/site-packages/django/contrib/admin/templates/jobs/job_2list.html, jobs/job_list.html (File does not exist)
可见查找顺序为/home/python/djproject/templates/jobs/job_2list.html, jobs/job_list.html
但是127.0.0.1:8000/1/访问正常,尽管urls.py里面设置的是tjobs/job_detail.html
实际访问的是jobs/job_detail.html
----------------------------------------------------------------------
Ref:
By default, the DetailView generic view uses a template called <app name>/<model name>_detail.html. In our case, it'll use the template "polls/poll_detail.html". The template_name argument is used to tell Django to use a specific template name instead of the autogenerated default template name.
https://docs.djangoproject.com/en/dev/intro/tutorial04/
上面的查找顺序是,先查找template_name设置的路径,然后查找默认路径即<app name>/<model name>_list.html和<app name>/<model name>_detail.html,如果能找到template_name设置的路径,如/home/python/djproject/templates/jobs/job_2list.html、/home/python/djproject/templates/jobs/job_2list.html、/home/python/djproject/templates/tjobs/job_detail.html,则使用指定的文件,否则再查找默认的路径,如果能找到则使用默认路径指向的文件即jobs/job_list.html和jobs/job_detail.html,如果再查找失败,则报上述错误。
模板载入器
https://docs.djangoproject.com/en/dev/ref/templates/api/#template-loaders
摘录:
By default, Django uses a filesystem-based template loader, but Django comeswith a few other template loaders, which know how to load templates from othersources.
Some of these other loaders are disabled by default, but you can activate themby editing yourTEMPLATE_LOADERS setting.TEMPLATE_LOADERSshould be a tuple of strings, where each string represents a template loaderclass. Here are the template loaders that come with Django:
-
django.template.loaders.filesystem.Loader
- Loads templates from the filesystem, according to TEMPLATE_DIRS.This loader is enabled by default. django.template.loaders.app_directories.Loader
-
Loads templates from Django apps on the filesystem. For each app inINSTALLED_APPS, the loader looks for a templatessubdirectory. If the directory exists, Django looks for templates in there.
This means you can store templates with your individual apps. This alsomakes it easy to distribute Django apps with default templates.
本文探讨了Django中模板的加载过程,包括如何指定模板名称、模板加载器的工作原理及模板查找顺序。同时,通过实例说明了当指定模板不存在时Django如何回退到默认模板。
462

被折叠的 条评论
为什么被折叠?



