how to include template

1,274 views
Skip to first unread message

Thiago Luiz Parolin

unread,
May 3, 2017, 10:00:37 AM5/3/17
to django...@googlegroups.com
I have a project that has a folder structure like this:

Project1 /
------Templates /
------------Base.html
------------Error.html
------App1
------------Templates /
------------------App1 /
------------------------App1.html

How can i include error.html in app1.html?

I am trying using {% include "../../../templates/error.html"%} but get error on page rendering:

The relative path '"../../../templates/error.html"' points outside the file hierarchy that template 'app1 / app1.html' is in.


I am totally new to django, python and programming.
Any help will be appreciated.

Dartos

unread,
May 3, 2017, 11:01:22 AM5/3/17
to Django users
Hi,

You need to set the template directories option in your project's settings file.

See here:
https://docs.djangoproject.com/en/1.11/topics/templates/#configuration

There's an example in the above link:
TEMPLATES = [
   
{
       
'BACKEND': 'django.template.backends.django.DjangoTemplates',
       
'DIRS': [
           
'/home/html/example.com',
           
'/home/html/default',
       
],
   
},
   
{
       
'BACKEND': 'django.template.backends.jinja2.Jinja2',
       
'DIRS': [
           
'/home/html/jinja2',
       
],
   
},
]

Once Django is aware of where to search for templates, you can include templates from your project's templates directory as follows:
{% include "Error.html" %}

And you can include app templates as follows:
{% include "App1/App1.html" %}



Also, pay attention to capitalization of filenames, as "Error.html" and "error.html" are different files on many systems. I use all-lowercase to avoid issues.

Dartos

Thiago Luiz Parolin

unread,
May 3, 2017, 1:44:31 PM5/3/17
to django...@googlegroups.com
Thank you...
All is working now!!!!
i was seeing in the wrong page of documentation..


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a61f65d2-5506-4750-a0c6-eb1be9663211%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages