Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load external html into html inside Django template

Tags:

python

django

I am trying to incorporate a django application into a web site where static html accounts for the majority. The directory structure is as follows.

root/
 ├ var/
 │  └ www/
 │   ├ html/
 │       ├ static
 │       │  ├style.css
 │       │  ├base.js
 │       │ 
 │       ├ web/
 │          ├head.html
 │          ├footer.html
 │          ├base.html
 │
 └ opt/
   └ django/
     ├ project/
     │
     ├ apps/
     ├ ├ views.py
       ├ template/
            ├ index.html

I want to make /opt/django/template/index.html read html in /var/www/html/web/. I do not know how to include.

{% include "/var/www/html/web/head.html" %}was useless. I do not want to change the directory structure.

like image 652
xKxAxKx Avatar asked Mar 16 '17 05:03

xKxAxKx


1 Answers

Considering this as your directory structure:

root/
 ├ var/
 │  └ www/
 │   ├ html/
 │       ├ static
 │       │  ├style.css
 │       │  ├base.js
 │       │ 
 │       ├ web/
 │          ├head.html
 │          ├footer.html
 │          ├base.html
 │
 └ opt/
   └ django/
     ├ project/
     │
     ├ apps/
     ├ ├ views.py
       ├ template/
            ├ index.html

To use /var/www/html/web/head.html in your index.html. Go to your settings.py and add this:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'apps/template'),'/var/www/html/web/']
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Now go to your index.html.

{% include "head.html" %}

I hope this will help.

like image 54
subha.py Avatar answered Nov 08 '22 00:11

subha.py