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

标签 python django django-templates

我有 chrono/chrono/templates/requests_app/request_list.html ,当 url(r'^$', BookListView.as_view()) 获取时加载打。 BookListView.as_view()class BookListView(ListView): model = Request。在 Django 中,它被告知要查找 chrono/chrono/templates/requests_app/request_list.html 吗?

例如,我可以将名称 request_list.html 更改为 foo_request_list.html,它会显示错误 request_list.html not found。所以我试图找到它的编码位置以查找 request_list.html。我查看了settings/base.py,没有提到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

最佳答案

这发生在导入django.views.generic.ListView所包含的代码中的某个位置。在 generic/list.py 中,MultipleObjectTemplateResponseMixin 是文件名最终组合在一起的地方。

代码像这样构建模板

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

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

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 本节涉及其他内容,但它们在示例中显示了覆盖模板名称。

关于python - Django 模板 - 在哪里编码以查找此特定模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20707381/

相关文章:

c# - 在opencv中检测到一个完整的椭圆

python - 将列表中的多个随机值加载到模板中

python - 自定义身份验证方法或者其他方法可以做到同样的事情? Django

python - django 脆皮表单按钮不显示

python - 在 Django 模板中隐藏重复迭代

python - 如何从另一个模块扩展模板?

Python - 从其他类中的方法检索值

python - 递归命名正则表达式组

python - 如何使用Python检查Azure容器中是否存在文件夹?

django - 如何在django分页中实现Post/Redirect/Get?