如何将外部html加载到Django模板中的html中

xKx*_*xKx 5 python django

我正在尝试将django应用程序合并到其中静态html占多数的网站。目录结构如下。

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
Run Code Online (Sandbox Code Playgroud)

我想在中/opt/django/template/index.html阅读html /var/www/html/web/。我不知道如何包括在内。

{% include "/var/www/html/web/head.html" %}没用 我不想更改目录结构。

sub*_*.py 5

将此视为您的目录结构:

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
Run Code Online (Sandbox Code Playgroud)

在 index.html 中使用/var/www/html/web/head.html。转到您的 settings.py 并添加以下内容:

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',
            ],
        },
    },
]
Run Code Online (Sandbox Code Playgroud)

现在转到您的 index.html。

{% include "head.html" %}
Run Code Online (Sandbox Code Playgroud)

我希望这将有所帮助。