Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Class-based Views Classy Views CRUD Views

rajan lagah
rajan lagah
2,539 Points

how TemplateDeleteView is connected with team_confirm_delete.html

class TemplateDeleteView(DeleteView): model = models.Team success_url = reverse_lazy("teams:list")

how this class know to open that template?

2 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Rajan,

The short answer is that the Django class DeleteView automatically adds the model name and appends "_confirm_delete.html".

The slightly longer version is (of course) in the documentation.

If you really want to get crazy, you can dig into the class definition of DeleteView, where you'll see two pertinent items:

template_name_suffix = '_confirm_delete'

and the method get_template_names(), which is fairly long, so I won't provide it in total. Here's the docstring for it:

Return a list of template names to be used for the request. May not be called if render_to_response is overridden. Returns the following list:

  • the value of template_name on the view (if provided)
  • the contents of the template_name_field field on the object instance that the view is operating upon (if available)
  • <app_label>/<model_name><template_name_suffix>.html

So if you override the template_name field, it will use that. Otherwise (except for the weird middle condition I don't really understand), it will default to the model name plus the template_name_suffix which is "confirm_delete".

Hope that's helpful!

Cheers :beers:

-Greg

Erika Suzuki
Erika Suzuki
20,299 Points

Good question. Who woulda thunk. There's a lot of magic going on here.