Python Django-如何添加';自动编号';列到由视图创建的表?

Python Django-如何添加';自动编号';列到由视图创建的表?,python,django,autonumber,Python,Django,Autonumber,我有一个Django项目,其中我的一个视图根据数据库中存储的信息显示了许多表。视图定义如下: def pipeline(request): ... tables = [] def make_table(detailed_status, projects, status_view=False, first_table=False): ... table_context_map = { Project.ds2: {'fi

我有一个Django项目,其中我的一个
视图
根据数据库中存储的信息显示了许多表。
视图
定义如下:

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)
我想做的是,在这个
视图创建的每个表的一列中,为表中的每一行插入一个“自动编号”。每当运行
视图
并加载网页时,表将根据数据库查询动态填充,我只想在创建表时对每个表中的项目列表进行编号

我该怎么做?我知道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>
<ul>
{% for data in data_list %}
    <li>{{ forloop.counter }}</li>
{% endfor %}
</ul>
可能就是你要找的

只需在模板中使用它,如下所示:

<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>
<ul>
{% for data in data_list %}
    <li>{{ forloop.counter }}</li>
{% endfor %}
</ul>
    {数据列表%中的数据为%0}
  • {{forloop.counter}}
  • {%endfor%}
对于您的文件,希望我的修改能起作用(用您的用户名标记):

{%load getters money\处理静态文件实用程序%}
{%if项目%}
{if total_i%}初始汇兑增值税:{{total_i|money:£“}{%endif%}
{如果总金额为%}最新的exc VAT:{{总金额:£}{其他%}
{%endif%}
数字@某人2088
{字段%中的字段为%1}
{%if字段。1%}
{%if sort==field.0,而不是reverse%}
{%else%}
{%endif%}
{%else%}
{{field.0}}
{%endif%}
{#使所有列(8)的数量相同#}
{%if-forloop.last%}
{%i在,,,,,,,,,%%中}
{%if-forloop.counter | add:forloop.parentloop.counter0<11%}
{%endif%}
{%endfor%}
{%if-detailed_status==“ds4”| ds%}
{%endif%}
{%endif%}
{%endfor%}
{user.employee.full|名称为:'Nick Ross'作为摘要_link%}
{项目%中的项目为%}
{%with initial_details=project.initial_details survey=project.survey%}
{打开灯箱}
{{forloop.counter}}@someone2088
{#ERF(22/11/2016@1450)添加一个计数器以显示表格行数}
{%if user.is_superuser%}

所以,如果理解正确,您可以从数据库中获取数据,并在渲染时添加一列指示行号?我的意思是,它只是为了显示目的吗?是的,我从数据库中获取数据,而行号只是为了显示目的。为了澄清,行号应该是显示表中的行号-它不应该从数据库中获取。因此@NeverWalkener answer是一种方法,感谢您的回答-听起来确实是我想要的,但我只是不确定如何将它合并到HTML中,因为它目前的状态似乎是将每个表作为一个整体显示,而不是作为行/列等。。。我已经更新了我的OP以显示当前表的HTML。我是否可以将您的
for
循环嵌套在已经存在的
for
循环中?i、 e
对于表中的表((对于表中的行){…})对于表中的表
@someone2088不确定这是否有效,但您可以尝试这样做:{%for tables%}{%for table%}{{r}}{%endfor%}{%endfor%}{%endfor%}。顺便说一句,你能显示管道视图的所有代码吗?
make_table
视图嵌套在
pipeline
视图中-我很确定我感兴趣的只是
make_table
中的代码,因为我只想更改表中显示的内容-页面的其余部分正在按我所希望的方式工作/显示,因此,我希望视图的其余部分是好的,并且不认为它对
make_table
视图显示的内容有任何影响……再看一遍,我认为还有一两行
make_table
我认为是相关的-我现在将它们粘贴进去。