dman
dman

Reputation: 11064

Django Template - Where is it coded to look for this specific template?

I have chrono/chrono/templates/requests_app/request_list.html which gets loaded when url(r'^$', BookListView.as_view()), gets hit. BookListView.as_view() is class BookListView(ListView): model = Request. When in Django is it being told to look for chrono/chrono/templates/requests_app/request_list.html?

For instance, I can change the name request_list.html to foo_request_list.html and it will say error request_list.html not found. So I am trying to find where it is coded to look for request_list.html. I looked in settings/base.py and there was no mention of 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

Upvotes: 1

Views: 115

Answers (1)

Daniel Rucci
Daniel Rucci

Reputation: 2872

This happens somewhere in code that is included by the import of django.views.generic.ListView. In generic/list.py, MultipleObjectTemplateResponseMixin is where the filename is finally put together.

The code builds the template like so

  • Folder prefix is the app name that this ListView is in
  • The part before _list.html is inferred by checking model.__name__, which is Request.__name__ in your example and will be Request by default and then lowercasing that.
  • _list is specified by ListView
  • .html is in MultipleObjectTemplateResponseMixin

If you don't like the file name it is giving you and you want to keep your app name as well as the name of the class you are specifying as your model you can override the above behavior in your 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 This section deals with other things but they show overriding the template name in their example.

Upvotes: 1

Related Questions