2017-10-06 25 views
1

在此先感謝。Django/template_block不運行

下面我有 「的index.html」

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

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

一個代碼,我有另一個HTML代碼( 「maintab.html」)爲 「index.html的」

孩子

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

,但它不工作.. 我接觸路徑是 「瀏覽器 - >」 http://localhost/encyclopedia/ 「(使用URL) - >功能高清(」 由view.py 「) - > 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)

+0

包括settings.py tepmlate部分,並添加'views.py' –

+0

什麼是你想實現?對於'

你好世界

'出現? –

+0

@JahongirRahmonov是的。我想調用index.html,然後粘貼maintab.html –

回答

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) 
+0

因此,在調用維護之後...然後調用index.html? –

+0

@ Yongwoon.jang,你不需要調用'index.html',它會在'maintab.html'顯示時自動繼承'index.html'。 –

+0

謝謝,但如果我需要index.html的另一個孩子?只需在index.html中添加{block ...}? –

0

您需要呈現maintab.html,其延伸index.html

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

好像你要包括mainlab.htmlindex.html

然後,更改mainlab.html這樣:

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

,然後在index.html

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

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