Skip to main content
 首页 » 编程设计

python之Django-如何将 'autonumber' 列添加到 View 创建的表中

2025年05月04日30daizhj

我有一个 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 创建的每个表的一列中,并在该列中为表中的每一行插入一个“autonumber”。每当运行 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(...) View 的文件的 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>&nbsp;</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>  

请您参考如下方法:

可能 forloop.counter正是您要找的。

只需像这样在您的模板中使用它:

<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>&nbsp;</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>