如何扩展并包括在组织Django模板中?

时间:2018-09-30 16:29:14

标签: django django-templates include extends

我的django模板文件夹中有这样的代码:

base.html

Switch(
    value: _switchValue, 
    onChanged: (bool value) {
        setState(() {
          _switchValue = value;   
        });
    },
)

top.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    {% block home %} {% endblock home %}
</body>
</html>

left.html

<div id="top">
    <h1>This is the navbar content</h1>
</div>

现在我想将每个模板都包含在这样的家庭模板中

home.html

<div id="left">
    <p>This is the menu content</p>
</div>

这是我在views.py中的渲染功能 views.py

{% extends "myapp/base.html" %}

{% block home %}
{% include "myapp/top.html" %}
{% include "myapp/left.html" %}
{% endblock home %}

但是当我运行服务器时,“ home.html”内部的include语句不起作用。发生了什么事?

谢谢。

1 个答案:

答案 0 :(得分:0)

enter image description here

  home.html
  {% extends "base.html" %}

  {% block home %}
  {% include "top.html" %}
  {% include "left.html" %}
  {% endblock home %}
 views.py
  def home(request):
      return render(request, 'home.html')
 urls.py (in your apps)

  from django.urls import path
  from .import views

  urlpatterns = [

  path('', views.home),
  ]

 urls.py (in your main folder)
  from django.contrib import admin
  from django.urls import path, include

 urlpatterns = [
 path('admin/', admin.site.urls),
 path('', include('your app name.urls')),
 ]

必须工作1000%,其他html文件相同