How to load external html into html inside Django template

I am trying to incorporate a django application into a website where static html are considered by most. 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 do /opt/django/template/index.htmlread html in /var/www/html/web/. I do not know how to turn it on.

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

+4
source share
3 answers

Given 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 the following:

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" %}

Hope this helps.

+2
source

/var/www/html/web/ DIRS .

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 ...
        },
    },
]

:

{% include "head.html" %}
0

Your templates can go anywhere. Your template directories are used with the DIRS parameter in the TEMPLATES setting in your settings file. For each application in INSTALLED_APPS, the loader searches for a subdirectory of templates. If the directory exists, Django searches for templates there.

note

Paths should use Unix backslashes, even on Windows.

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

Source: https://habr.com/ru/post/1672423/


All Articles