Documentation of app_directories.Loader

24 views
Skip to first unread message

Per-Olof Åstrand

unread,
Jan 5, 2013, 10:17:14 AM1/5/13
to django...@googlegroups.com
I had a problem interpreting the documentation of app_directories.Loader in https://docs.djangoproject.com/en/dev/ref/templates/api/#loading-templates (I use the dev version of Django). As I read the documentation, I should put the templates for an app in

app_name/templates/*.html

whereas to get it to work, I had to put them in

app_name/templates/app_name/*.html

It also seems like it looks for templates in (which I could see from the error message when failing with the first approach)

app_name1/templates/app_name2/*.html

I can see the power of that when having a hierarchy of apps. Am I missing something here?

Per-Olof

Amirouche

unread,
Jan 8, 2013, 7:50:17 PM1/8/13
to django...@googlegroups.com
Héllo Per-Olof,


On Saturday, January 5, 2013 4:17:14 PM UTC+1, Per-Olof Åstrand wrote:
I had a problem interpreting the documentation of app_directories.Loader in https://docs.djangoproject.com/en/dev/ref/templates/api/#loading-templates (I use the dev version of Django). As I read the documentation, I should put the templates for an app in

app_name/templates/*.html


Yes, it should work if you reference the template *without* the app_name for instance 

{% extends "base.html" %}
{% include "footer.html" %}

or in python with «render(request, "base.html", ctx)» but you never render the base html or footer directly so this is dummy example
 
whereas to get it to work, I had to put them in

app_name/templates/app_name/*.html


it only works if you reference the app_name when you call the template for instance «render(request, "app_name/index.html", ctx)» same for include and extends.

It's a convention to avoid clash between templates names, it a way to namespace templates, otherwise you would need to namespace the names of the templates like: «app_name_index.html» which is not nice...

Per-Olof Åstrand

unread,
Jan 11, 2013, 7:20:17 PM1/11/13
to django...@googlegroups.com
Thanks for your feedback. It is exactly the way you suggest. I did not think through what I had done myself (referring to "app_name/*.html" in my views and templates) before posting.

Per-Olof
Reply all
Reply to author
Forward
0 new messages