【问题标题】:Unable to display the content of {% block content %}{% endblock %} in Django无法在 Django 中显示 {% block content %}{% endblock %} 的内容
【发布时间】:2020-02-04 20:45:09
【问题描述】:

我无法在我的网页上显示 {% block content %}{% endblock %} 的内容。

这是树形结构:

  • 工具箱/
    • 工具箱/
      • 丽莎/
        • 模板/
          • 丽莎/
            • lisa.html
      • 模板/
        • base.html

这是base.html的代码:

{% load static %}
<!DOCTYPE html>
<html lang="fr">
<head>
</head>
<body>
    <section id="main-content">
        {% block content %}{% endblock %}
    </section>
</body>
</html>

这里是lisa.html的代码:

{% extends 'templates/base.html' %}

{% block content %}
    <h2>Bienvenue !</h2>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec rhoncus 
        massa non tortor. Vestibulum diam diam, posuere in viverra in, 
        ullamcorper et libero. Donec eget libero quis risus congue imperdiet ac 
        id lectus. Nam euismod cursus arcu, et consequat libero ullamcorper sit 
        amet.
    </p>
{% endblock %}

你知道它可能来自哪里吗?

提前谢谢你

【问题讨论】:

  • 几乎可以肯定,您的视图呈现的是父级而不是子级;否则你会得到扩展块的 TemplateNotFound 异常。

标签: python django templates tags


【解决方案1】:

尝试改变:

{% extends 'templates/base.html' %}

收件人:

{% extends 'base.html' %}

【讨论】:

  • 我只是修改它并没有改变任何东西。
【解决方案2】:

@Priyanshu Gupta 谢谢你的回答,看我的树结构和你给我的很相似。 所以这是我在“setting.py”文件中的“模板”部分:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            # Cette ligne ajoute le dossier templates/ à la racine du projet
            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',
            ],
        },
    },
]

【讨论】:

  • 删除'DIRS':[os.path.join(BASE_DIR, 'templates')]。相反,它应该只是'DIRS':['templates']
  • 这个答案应该是对问题的更新。
  • 我将"'DIRS':[os.path.join (BASE_DIR, "templates'),]" 更改为"'DIRS': ['templates']," 但是我仍然可以' t 显示我的 lisa.html 的内容
  • 我找到了解决方案,我把“base.html”放在“views.py”中,而我不得不把“lisa/lisa.html”
【解决方案3】:

您的项目结构应如下所示:

toolbox
├── lisa
│   ├── templates
|   |   └── lisa
│   │       └── lisa.html
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── templates
│   └── base.html
├── toolbox
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

在你的 settings.py 中:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['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',
        ],
    },
},

]

  1. 我假设在您的 settings.py 中您已将模板目录设置为 templates
  2. 在 lisa.html 中只写 {% extends 'base.html' %}。通过这样做,Django 搜索存在的templates/base.html,因此它应该可以工作。
  3. 您的代码不工作的原因是当您编写{% extends 'templates/base.html' %} 时,Django 搜索实际上不存在的templates/templates/base.html

【讨论】:

    【解决方案4】:

    1.尝试对 urls.py forder 中的 url 进行仔细检查,例如: 例如:

        urlpatterns = [
            path('', views.home,),
            path('lisa', views.lisa,),
    

    2.那么它在url上不起作用:http://localhost:8000/ 你必须仔细检查它是:http://localhost:8000/lisa

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      • 2022-01-27
      相关资源
      最近更新 更多