无法在Djang上扩展html模板

2024-04-26 23:32:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我不能扩展一个html文档(子基_主页.html)到另一个父html文档-基本.html 我有下一个Django项目结构:

Testdjango/
blog/
   migrations/
   templates/
       blog/
          base.html
          base_home.html
   __init.py__
   admin.py
   apps.py
   models.py
   tests.py
   urls.py
   views.py
db.sqlite3
manage.py

我的父模板是基本.html,下一个代码:

^{pr2}$

还有我的“孩子”基地_主页.html有下一个代码:

{% extends "blog/base.html" %}
{% block title %}
Home
{% endblock %}
{% block content %}
Lorem bla bla
{% endblock %}

在页面上,我看不到基地的任何东西_主页.html在


Tags: 项目django代码文档pybasehtmlblog
3条回答

试试看

{% extends "base.html" %}

而不是

^{pr2}$

你的模板看起来不错,确保你的模板设置看起来像这样,而且你已经将应用程序添加到INSTALLED_APPS。在

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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',
            ],
        },
    },
]

由于base.htmlbase_home.html在同一个方向上,所以扩展base as

^{pr2}$

可以用两种方式扩展模板

  1. include在模板中包含一个相关的html部分
  2. extends将普通html扩展到diff页面中

在基本.html在

<html>
<head></head>
<body>
======
======
</body>
</html>

home.html

If base.html in any folder please mention folder name dont put slash(/) before the foldername `{% extends 'foldername/base.html' %}` 

{% extends 'base.html' %} ## you can see the base.html styles in the home.html

假设您需要包含一个相关的div、表或代码使用部分 {% include 'base.html' %}如果它在文件夹{% include 'foldername/base.html' %}

相关问题 更多 >