xKxAxKx
xKxAxKx

Reputation: 1074

How to load external html into html inside Django template

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.

Upvotes: 7

Views: 13533

Answers (3)

metmirr
metmirr

Reputation: 4312

Your templates can go anywhere you want. Your template directories are by using the DIRS option in the TEMPLATES setting in your settings file. For each app in INSTALLED_APPS, the loader looks for a templates subdirectory. If the directory exists, Django looks for templates in there.

note

Paths should use Unix-style forward slashes, even on Windows.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            '/var/www/html/web/',
        ],
    },
]

Upvotes: 0

subha.py
subha.py

Reputation: 473

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.

Upvotes: 5

ChidG
ChidG

Reputation: 3223

Add /var/www/html/web/ to the DIRS option in the template configuration dictionary in your project settings.

https://docs.djangoproject.com/en/1.10/ref/settings/#std:setting-TEMPLATES-DIRS

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['/var/www/html/web/'], # won't work on Windows
        'APP_DIRS': True,
        'OPTIONS': {
            # ... some options here ...
        },
    },
]

Then:

{% include "head.html" %}

Upvotes: 0

Related Questions