【问题标题】:Django template extends doens`t open a fileDjango模板扩展不打开文件
【发布时间】:2017-08-30 09:08:33
【问题描述】:

我正在尝试从另一个扩展一个 html 文件,但它只显示 base.html 结果。

这是我的项目文件夹:

-diploma
  -catalogue
    -migrations
    -templates
      -catalogue
        base.html
        header.html
    _init__.py
    admin.py
    apps.py
    models.py
    tests.py
    urls.py
    views.py
  -diploma
    __init__.py
    settings.py
    urls.py
    wsgi.py

settings.py

...

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

INSTALLED_APPS = [
    'catalogue',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

...

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

urls.py

from django.conf.urls import url
from catalogue import views

urlpatterns = [
     url(r'^$', views.index, name='index'),
]

views.py

from django.shortcuts import render

def index(request):
     return render(request, 'catalogue/base.html')

base.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {% block content %}
    {% endblock %}
</body>
</html>

header.html

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

{% block content %}
    <h1>Hello</h1>
{% endblock %}

真的,试图找到问题,但无法弄清楚。仅影响在 base.html 中进行的更改

【问题讨论】:

  • ..因为你正在渲染基础文件?
  • 天啊,笨蛋...谢谢正文

标签: python django python-3.x django-templates


【解决方案1】:

尝试放置: {% 扩展 'base.html' %} 而不是整个路径

【讨论】:

    猜你喜欢
    • 2021-10-08
    • 2010-11-27
    • 1970-01-01
    • 2015-03-03
    • 2012-01-31
    • 2017-04-24
    • 2017-04-10
    • 2012-01-10
    相关资源
    最近更新 更多