如何在一个模板中插入多个Django块? [英] How to insert multiple Django blocks into one template?
问题描述
我试图通过不同的块或包含方法在一个模板中使用更多不同的视图,但是我陷入了困境.我的目标是将我的不同观点整合到一个模板中.我尝试了更多解决方案,但这些方法对我没有用.我展示了这些解决方案:
我的观点:
def console_data(request):#在这里,我从PostgreSQL数据库中收集数据,然后将它们压入#Objects,然后通过如下所示的渲染将其发送到我的test_a.html.返回渲染(请求,'my_project/test_a.html',send_data)def chart_data(request):#在这些视图中,我的最终目标是从数据库收集数据并创建#charts,但为简单起见,我只使用一个列表(a = [1,2,3,4]),而我#将其推入test_b.html,然后进行渲染.渲染(请求,'my_project/test_b.html',send_data))
我的模板:
1,base.html
<!DOCTYPE html>< html lang ="zh-CN">< head>{%block title%}< title>我的项目</title> {%endblock%}< meta charset ="utf-8">< meta name ="viewport" content ="width = device-width,initial-scale = 1"><!-包含CSS文件->{%静态负载%}<!-包含CSS文件->< 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><身体>{%block sidebar%}{%endblock%}{%封锁内容%}{%endblock%}</body></html>
1,test_a.html
{%扩展了"monitor/base.html"%}{%block content%}{%for表中的我%}< 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(test1):
{%block conent%}{%endblock%}{%方块图%}{%endblock%}
1,test_b.html(test1):
{%扩展了"monitor/base.html"%}{%block chart%}{%for a in%}< p>{{i}}</p>{%endfor}{%endblock%}
B :我尝试使用 {{block.super}}
1,base.html(test1):
{%block conent%}{%endblock%}
1,test_b.html(test1):
{%扩展了"monitor/base.html"%}{%block content%}{{block.super}}{%for a in%}< p>{{i}}</p>{%endfor}{%endblock%}
C:最后,我也使用了include方法,但我也没有为我工作.在这里,我尝试在test_b.html中使用简单的html标记(例如(< p> Hello world</p>
))进行工作.因此,如果我不使用变量,则可以使用,但是必须从我的视图中使用变量.
1,test_a.html
{%扩展了"monitor/base.html"%}{%block chart%}{%for a in%}< p>{{i}}</p>{%endfor}{%endblock%}{%包括"my_project/test_b.html"%}
2,test_b.html
{%for i in a%}< p>{{i}}</p>{%endfor%}
如@ alessioferri20所述,您不能同时渲染2个视图.基本上,一个视图对应一个HTTP请求&响应.
您可以使用自定义模板标签来实现所需的目标: https://docs.djangoproject.com/en/2.0/howto/custom-template-tags/
但是我必须从视图中使用变量.
例如, chart_data
可以是一个包含标记( https://docs.djangoproject.com/en/2.0/howto/custom-template-tags/#inclusion-tags ),下面是一些伪代码:>
假设您要从视图中显示 age_stats
的图表:
def your_view(request,...):age_stats = {...}....返回render(request,"template ...",{'age_stats':age_stats})
在您的模板中:
{%chart age_stats%}
在模板标签中:
@ register.inclusion_tag('path-to/chart.html')def图表(input_data):#从数据库中收集数据&使用来自您的视图的input_data数据= ...返回{'data':data}
I am trying to use more different views in one template via different blocks or include method but I have got stuck. My aim is to implement my different views into one template. I tried to more solution but these haven't worked for me. I show these solution:
My views:
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))
My templates:
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, test b.html
{%for i in a%}
<p> {{i}}</p>
{%endfor}
So my goal would be that the test_b.html's result could appear in the base.html.
My solutions:
A: I tried to put into a block the test_b.html and integrate in the base.html
1, base.html (test1):
{% 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: I tried to use {{ block.super }}
1, base.html (test1):
{% 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: Finally I used include method too but I didn't work for me as well. Here I tried to use simple html tags like (<p>Hello world</p>
) int the test_b.html and worked. So If I do not use variables it works but I have to use my variables from my views.
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 %}
As @alessioferri20 mentioned, you cannot render 2 views at the same time. Basically, one view corresponds to one HTTP request & response.
You can achieve what you want by using custom template tags: https://docs.djangoproject.com/en/2.0/howto/custom-template-tags/
but I have to use my variables from my views.
For example, chart_data
could be an inclusion tag (https://docs.djangoproject.com/en/2.0/howto/custom-template-tags/#inclusion-tags), here is some pseudo-code:
Let's say you want to show a chart for the age_stats
from your view:
def your_view(request, ...):
age_stats = {...}
....
return render(request, "template...", {'age_stats': age_stats})
In your template:
{% chart age_stats %}
In your template tags:
@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}
这篇关于如何在一个模板中插入多个Django块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!