【发布时间】:2020-09-22 07:54:28
【问题描述】:
在一个 Django 应用程序中,我有几个结构相似的 html 页面。我已经为我的所有项目使用了 base_site.html 模板,但对于这些其他页面,我想使用第二个模板来创建表格。这些表可能比其他表有更多或更少的行和列。
我的想法是views.py 中的函数将发送任何给定页面的标题列表以及数据字典以填充表格,然后在 html 页面中我将迭代标题列表以放置标题然后我会迭代 dict 以用数据填充表。
我怎样才能有一个 table-template.html 来使用 headers 中的每个函数的变量 views.py 每当它们被调用时?
类似table-template.html
<h1>TEST</h1>
<table>
<tr bgcolor="#ccc">
<th>{% for h in headers %} {{ h }} {% endfor %}</th>
</tr>
</table>
考虑到views.py 中的每个函数都会返回自己的headers 列表
那么如何在任何html页面中使用它
{% extends "admin/base_site.html" %}
{% load static %}
{% block content %}
{% include "table-template.html" %}
{% load static %}
{% for key,value in mydict.items %}
<table>
<tr>
<td>{{ value }}</td>
</tr>
</table>
{% endfor %}
【问题讨论】:
标签: django django-views django-templates