Django模板 - 它在哪里编码以查找此特定模板?

时间:2013-12-20 15:54:36

标签: python django django-templates

chrono/chrono/templates/requests_app/request_list.htmlurl(r'^$', BookListView.as_view())加载后,BookListView.as_view()被点击。 class BookListView(ListView): model = Requestchrono/chrono/templates/requests_app/request_list.html。在Django中被告知要查找request_list.html

例如,我可以将名称foo_request_list.html更改为request_list.html,但会发现错误from django.conf.urls import patterns, include, url from django.views.generic import TemplateView, ListView from requests_app.views import BookListView from django.contrib.auth.views import login from requests_app.forms import LoginForm # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # url(r'^$', TemplateView.as_view(template_name='base.html')), url(r'^$', BookListView.as_view()), url(r'^login/$', login, {'authentication_form': LoginForm}, name='login'), # Examples: # url(r'^$', 'chrono.views.home', name='home'), # url(r'^chrono/', include('chrono.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: url(r'^admin/', include(admin.site.urls)), ) from django.views.generic.base import TemplateView, RedirectView from django.http import HttpResponse from django.views.generic import ListView from requests_app.models import Request, Region from django.core.urlresolvers import reverse class BookListView(ListView): model = Request 未找到。所以我试图找到它编码的位置以查找request_list.html。我查看了settings / base.py,没有提到request_list.html。

{{1}}

1 个答案:

答案 0 :(得分:1)

这种情况发生在导入django.views.generic.ListView的代码中。在generic/list.py中,MultipleObjectTemplateResponseMixin是文件名最终放在一起的位置。

代码构建模板如此

  • 文件夹前缀是此ListView所在的应用名称
  • _list.html之前的部分是通过检查model.__name__来推断的,Request.__name__在您的示例中为_list,默认情况下为请求,然后小写。
  • ListView.html
  • 指定
  • MultipleObjectTemplateResponseMixin位于BookListView

如果您不喜欢它提供给您的文件名,并且您希望保留您的应用名称以及您指定为模型的类的名称,则可以覆盖{{1}中的上述行为}

class BookListView(ListView):
    model = Request
    template_name = "books/foo_request_list.html"

https://docs.djangoproject.com/en/1.6/topics/class-based-views/generic-display/#viewing-subsets-of-objects 本节介绍其他内容,但它们会在示例中显示覆盖模板名称。