如何将多个 Django 块插入到一个模板中?

How to insert multiple Django blocks into one template?

我试图通过不同的块或包含方法在一个模板中使用更多不同的视图,但我遇到了困难。我的目标是将我的不同观点实施到一个模板中。我尝试了更多解决方案,但这些对我没有用。我展示了这些解决方案:

我的观点:

def  dashboard_data(request):
    # Here I collect data from my PostgreSQL database I push these into 
    #Objects and I send it to my test_a.html via render like below.

    return render(request, 'my_project/test_a.html', send_data)

def chart_data(request):
    #In these view my final goal is to collect data from database and create 
    #charts but for the simplicity I just use a list (a=[1,2,3,4]) and I 
    #push it to the test_b.html and I render it.


    render(request, 'my_project/test_b.html', send_data))

我的模板:

1,base.html

<!DOCTYPE html>
<html lang="en">
<head>

  {% block title %}<title>My project</title>{% endblock %}
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- Include CSS files -->
  {% load static %}
  <!-- Include CSS files -->
  <link rel="stylesheet" href="{% static 'css/bootstrap.css' %}">
  <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
  <link rel="stylesheet" href="{% static 'css/bootstrap-table.css' %}">
  <link rel="stylesheet" href="{% static 'css/styles.css' %}">
  <link rel="stylesheet" href="{% static 'css/font-awesome.min.css' %}">
</head>

<body>

     {% block sidebar %}

     {% endblock %}

     {% block content%}
     {% endblock %}

</body>
</html> 

1,test_a.html

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

  {%block content%}

  {%for i in table%}
       <p> {{i.record}}</p>
  {%endfor%} 

  {%endblock}

2、测试b.html

  {%for i in a%}
       <p> {{i}}</p>
  {%endfor}

所以我的目标是让 test_b.html 的结果出现在 base.html.

我的解决方案:

A:我试图将 test_b.html 放入一个块中并集成到 base.html

1, base.html (测试1):

 {% block conent%}

 {% endblock %}

 {% block chart%}

 {% endblock %}

1, test_b.html (test1):

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

{%block chart%}    

{%for i in a%}
       <p> {{i}}</p>
{%endfor}

{%endblock%}

B:我试过用{{ block.super }}

1, base.html (测试1):

 {% block conent%}

 {% endblock %}

1, test_b.html (test1):

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

{%block content%}    
 {{ block.super }} 
{%for i in a%}
       <p> {{i}}</p>
{%endfor}

{%endblock%}

C: 最后我也使用了 include 方法,但我也没有用。在这里,我尝试使用简单的 html 标签,例如 (<p>Hello world</p>) int the test_b.html 并且成功了。因此,如果我不使用变量,它会起作用,但我必须使用我的观点中的变量。

1, test_a.html

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

{%block chart%}    

{%for i in a%}
      <p> {{i}}</p>
{%endfor}

{%endblock%}
{% include "my_project/test_b.html" %} 

2、test_b.html

{%for i in a%}
     <p> {{i}}</p>
{% endfor %}

要包含来自另一个文件的 HTML 文件,只需使用 {% include 'htmlfilename.hml' %} 如果它是基本模板,则在 {% block your_block_name %}{% endblock %} 之外。

您可以了解更多 here

<body>
     {% include 'header.html' %}
     {% block sidebar %}{% endblock %}
     {% block content%}{% endblock %}
     {% include 'footer.html' %}
</body>

如果您想跨多个模板使用某些功能,请使用 inclusion_tags

如@alessioferri20 所述,您不能同时渲染 2 个视图。基本上,一个视图对应一个 HTTP 请求和响应。

您可以使用自定义模板标签实现您想要的效果: https://docs.djangoproject.com/en/2.0/howto/custom-template-tags/


but I have to use my variables from my views.

例如,chart_data可以是一个包含标签(https://docs.djangoproject.com/en/2.0/howto/custom-template-tags/#inclusion-tags),这里是一些伪代码:

假设您想显示 age_stats 从您的角度看的图表:

def your_view(request, ...):
    age_stats = {...}
    ....
    return render(request, "template...", {'age_stats': age_stats})

在您的模板中:

{% chart age_stats %}

在您的模板标签中:

@register.inclusion_tag('path-to/chart.html')
def chart(input_data):
    # collect data from database & use the input_data coming from your view
    data = ...
    return {'data': data}