Django / template_block不会被操作

时间:2017-10-06 05:22:14

标签: django django-templates

提前致谢。

我在“index.html”下面有一段代码

<!DOCTYPE html>
<html>
<head>
</head>

<body>
	{% block content %}{% endblock %}
</body>
</html>

我为“index.html”的孩子提供了另一个html代码(“maintab.html”)

{% extends "encyclopeida/index.html" %}
{% block content %}
<p>Hello world</p>
{% endblock %}

但它不起作用.. 我的联系路径是 “浏览器 - &gt;”http://localhost/encyclopedia/“(按网址) - &gt;功能def(”by view.py“) - &gt; index.html”

以下是我的文件夹结构。

link

最后这是我的setting.py和view.py

# Application definition

INSTALLED_APPS = [
    'encyclopedia.apps.EncyclopediaConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'projectstarbucks.urls'

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

WSGI_APPLICATION = 'projectstarbucks.wsgi.application'

from django.shortcuts import render

from .models import Content, Service

# Create your views here.
def index(request):
    source_list = Content.objects.order_by('id')
    menu_list = Service.objects.order_by('id')
    context = {'source_list':source_list,'menu_list':menu_list}
    return render(request, 'encyclopedia/index.html', context)

3 个答案:

答案 0 :(得分:0)

您需要呈现maintab.html扩展index.html

return render(request, 'path/to/maintab.html' ,  context) 

答案 1 :(得分:0)

你没有在这里传递maintab.html

def index(request):
    source_list = Content.objects.order_by('id')
    menu_list = Service.objects.order_by('id')
    context = {'source_list':source_list,'menu_list':menu_list}
    return render(request, 'encyclopedia/maintab.html', context)

答案 2 :(得分:0)

好像你想在mainlab.html内包含index.html

然后,将mainlab.html更改为:

{% block content %}
    <p>Hello world</p>
{% endblock %}

然后在index.html

<!DOCTYPE html>
<html>
<head>
</head>

<body>
    {% include "maintab.html" %}
</body>
</html>