Template file extension

I’m following a tutorial from a text and I am at a section on using the template system. In the text the templates use an .xhtml extension such as login.xhtml. The code fails with TemplateNotFound and it is looking for application/templates/registration/login.html. If I change the extension to .html the code works fine. My question is does Django only look for the .html, or is there some way to get it to also search for .xhtml? The text was published in 2022 so not really dated.

Django itself doesn’t care what the filename (including the extension) is. You can use any extension you want.

If you’re rendering a template in a view, you’re specifying a complete file name, with the extension. So if the code is looking for login.html, that’s what you would call the file.

Yes. This also means you can use any extension. If you want to name your templates with a different naming convention such as ‘.template’, that works too.

I’m assuming this book is referencing Django’s provided authentication pages, in which case it is looking for a file named login.html.

Thanks for taking the time to address my question. The tutorial is in fact using Django’s built in views so the template name in never explicitly used by the code in a render function. As you say Django by default is looking for a template named login.html. After some more research (and sleep) I was able to uncover that if I really really wanted to use login.xhtml I could pass the template name in the as_view() call like:

path('login/', auth_views.LoginView.as_view(template_name='registration/login.xhtml'), name='login'),

This would also be a method of changing the location of the template if so desired.

This technically is not an accurate statement. The template name is used in a render function, just not one that you supply.

Side note: I’d be really suspect of a tutorial published in the last year making such a mistake in the absense of detail, explanation, or qualification. The default template name has been login.html for as long as I can remember - I’m guessing at least 10 years.

For what it’s worth Django 4 By Example - Fourth Edition by Antonio Melé, packt publishing, August 2022… There is a github repository of all the code from the book. I took a look at the repository for the chapter in question and it uses .html vs .xhtml. Your right, it is a pretty sloppy oversite.