Can Django template use relative path in 'include' tag?

2,122 views
Skip to first unread message

Wei Litao

unread,
Jun 2, 2006, 12:13:34 AM6/2/06
to django...@googlegroups.com
I have a template which reside in 'myapp/tmpl1.html'. In this template I want to 'include' another template which reside in 'myapp/tmpl2.html'. But I don't want to use the syntax as '{% include "myapp/tmpl2.html" %}' . Instead, i want simply use something like '{% include " tmpl2.html" %}' , because tmpl2.html lived in the same folder as tmpl1.html. Is it possible to do that?


--
Sincerely,

Wei Litao

James Bennett

unread,
Jun 2, 2006, 1:11:28 AM6/2/06
to django...@googlegroups.com

The 'include' tag can accept relative paths. The 'ssi' tag, however,
only accepts absolute paths.


--
"May the forces of evil become confused on the way to your house."
-- George Carlin

Wei Litao

unread,
Jun 2, 2006, 2:47:39 AM6/2/06
to django...@googlegroups.com
On 6/2/06, James Bennett <ubern...@gmail.com> wrote:

On 6/1/06, Wei Litao <wlt...@gmail.com> wrote:
> I have a template which reside in 'myapp/tmpl1.html'. In this template I
> want to 'include' another template which reside in 'myapp/tmpl2.html'. But I
> don't want to use the syntax as '{% include "myapp/tmpl2.html" %}' .
> Instead, i want simply use something like '{% include " tmpl2.html" %}' ,
> because tmpl2.html lived in the same folder as tmpl1.html. Is it possible to
> do that?

The 'include' tag can accept relative paths. The 'ssi' tag, however,
only accepts absolute paths.

This relative is just relative to 'TEMPLATE_DIRS', not relative the current template.

--
"May the forces of evil become confused on the way to your house."
  -- George Carlin




--
Sincerely,

Wei Litao

Ivan Sagalaev

unread,
Jun 2, 2006, 2:56:17 AM6/2/06
to django...@googlegroups.com
Wei Litao wrote:

{% include %} searches templates not by directory structure. It follows
Django's mechanism of template loaders
(http://www.djangoproject.com/documentation/templates_python/#loader-types)
that can search in designated template directories, in application's
internal template directories in egg files, in database. For example
you can have two apps with template directories:

app1/
templates/
somedir/
base1.html

app2/
templates/
somedir/
base2.html

And those templates will be known by names 'somedir/base1.html',
'somedir/base2.html' though they are not in the same directory physically.

I'd recommend to think about template structure not as some physical
files in directories but as some virtual tree structure that doesn't
have a concept of "same" directory and always requires you to use
explicit full template names.

Reply all
Reply to author
Forward
0 new messages