我有一个Django项目,其中一个views
根据存储在数据库中的信息显示了许多表。 view
定义如下:
def pipeline(request):
...
tables = []
def make_table(detailed_status, projects, status_view=False, first_table=False):
...
table_context_map = {
Project.ds2: {'fields': [['date added',1], ['site visit date',1], ['initial exc VAT',1]]},
...
# Similar lines to populate the tables with data from the database
...
}
table_context = table_context_map[detailed_status]
...
table_context['fields'] = [['project name',1], ['town',1], ['postcode',1], ['contact name',1]] + table_context['fields']
table = render_to_string('.../....html', table_context)
...
return render(request, 'abc.html', context)
我想做的是,在view
创建的每个表的列中,并在该列中为表中的每一行插入一个“自动编号”。每当运行view
并加载网页时,将根据数据库查询动态填充表格,我只想在每个表格中创建项目列表。
我该怎么做?我知道Python Django,所以任何帮助或指导都会非常感激。
修改
当前在网页中显示这些表格的HTML部分如下所示:
<div class="content">
{% block tables %}
{% for table in tables %}
{# Only shows table headers on first table (css first of type on multisection thead) #}
{{table}}
{% endfor %}
{% endblock tables %}
</div>
修改
传递到render_to_string(...)
视图的文件的HTML具有以下结构:
{% load getters money_handling staticfiles utilities %}
{% if projects %}
<div class="table-container m-t-lg">
<table class="multisection pipeline left">
<tr class="sub-summary">
<th colspan="4"><a href="?detailed_status={{detailed_status}}"><h3 class="p-l-sm">{{detailed_status_str}}</h3></a></th>
{% if total_i %}<th>Initial exc VAT: {{total_i|money:"£"}}</th>{% endif %}
{% if total_u %}<th>Latest exc VAT: {{total_u|money:"£"}}</th>{% else %}
<th></th>
{% endif %}
</tr>
</table>
<table class="multisection pipeline left m-b-xl">
<tr class="summary">
<th style="width: 3em;"></th>
{% for field in fields %}
<th class="text-sm p-l-sm p-t-sm p-b-sm" style="width:{{widths|getval:forloop.counter0}}">
{% if field.1 %}
{% if sort == field.0 and not reverse %}
<a href="?sort=-{{field.0}}&detailed_status={{detailed_status}}">{{field.0}}</a>
{% else %}
<a href="?sort={{field.0}}&detailed_status={{detailed_status}}">{{field.0}}</a>
{% endif %}
{% else %}
{{field.0}}
{% endif %}
</th>
{# Make all have the same number of columns (8) #}
{% if forloop.last %}
{% for i in ',,,,,,,,' %}
{% if forloop.counter|add:forloop.parentloop.counter0 < 11 %}
<th> </th>
{% endif %}
{% endfor %}
{% if detailed_status == "ds4"|ds %}
<th></th>
{% endif %}
{% endif %}
{% endfor %}
</tr>
{% with user.employee.full_name|is:'Nick Ross' as summary_link %}
{% for project in projects %}
<tr data-project-id="{{project.id}}" class="even {% if project.office == 2 %} col{% endif %}">
{% with initial_details=project.initial_details survey=project.survey %}
{# Open lightbox #}
<td>
{# ERF(22/11/2016 @ 1450) Add a counter to display table row numbers #}
{% if user.is_superuser %}
<a class="gallery-loader" data-project-id="{{project.id}}"><i class="icon info"></i></a>
{% if forloop.first and first_table %}
<div id="iframe_gallery_wrap">
<a href="#p1" class="gallery">
<div id="p1">
<iframe class="lightbox-content" src="{% url 'projects:description' project.id %}report/" width="1200" height="800" id="p1" style="border:none;" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
</div>
</a>
答案 0 :(得分:6)
Probaly forloop.counter正是您所寻找的。 p>
只需在模板中使用它:
<ul>
{% for data in data_list %}
<li>{{ forloop.counter }}</li>
{% endfor %}
</ul>
至于你的文件希望我的修改能够起作用(用你的用户名标记):
{% load getters money_handling staticfiles utilities %}
{% if projects %}
<div class="table-container m-t-lg">
<table class="multisection pipeline left">
<tr class="sub-summary">
<th colspan="4"><a href="?detailed_status={{detailed_status}}"><h3 class="p-l-sm">{{detailed_status_str}}</h3></a></th>
{% if total_i %}<th>Initial exc VAT: {{total_i|money:"£"}}</th>{% endif %}
{% if total_u %}<th>Latest exc VAT: {{total_u|money:"£"}}</th>{% else %}
<th></th>
{% endif %}
</tr>
</table>
<table class="multisection pipeline left m-b-xl">
<tr class="summary">
<th style="width: 3em;"></th>
<th>Number</th> @someone2088
{% for field in fields %}
<th class="text-sm p-l-sm p-t-sm p-b-sm" style="width:{{widths|getval:forloop.counter0}}">
{% if field.1 %}
{% if sort == field.0 and not reverse %}
<a href="?sort=-{{field.0}}&detailed_status={{detailed_status}}">{{field.0}}</a>
{% else %}
<a href="?sort={{field.0}}&detailed_status={{detailed_status}}">{{field.0}}</a>
{% endif %}
{% else %}
{{field.0}}
{% endif %}
</th>
{# Make all have the same number of columns (8) #}
{% if forloop.last %}
{% for i in ',,,,,,,,' %}
{% if forloop.counter|add:forloop.parentloop.counter0 < 11 %}
<th> </th>
{% endif %}
{% endfor %}
{% if detailed_status == "ds4"|ds %}
<th></th>
{% endif %}
{% endif %}
{% endfor %}
</tr>
{% with user.employee.full_name|is:'Nick Ross' as summary_link %}
{% for project in projects %}
<tr data-project-id="{{project.id}}" class="even {% if project.office == 2 %} col{% endif %}">
{% with initial_details=project.initial_details survey=project.survey %}
{# Open lightbox #}
<td>{{ forloop.counter }}</td> @someone2088
<td>
{# ERF(22/11/2016 @ 1450) Add a counter to display table row numbers #}
{% if user.is_superuser %}
<a class="gallery-loader" data-project-id="{{project.id}}"><i class="icon info"></i></a>
{% if forloop.first and first_table %}
<div id="iframe_gallery_wrap">
<a href="#p1" class="gallery">
<div id="p1">
<iframe class="lightbox-content" src="{% url 'projects:description' project.id %}report/" width="1200" height="800" id="p1" style="border:none;" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
</div>
</a>