someone2088 someone2088 - 13 days ago 4
Python Question

Django- how to add 'autonumber' column to tables created by view?

I have a Django project, in which one of my

views
is displaying a number of tables based on information stored in the database. The
view
is defined as follows:

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)


What I'd like to do, is at a column to each table created by this
view
, and insert an 'autonumber' in that column for every row in the table. The tables will be populated dynamically, based on a database query whenever the
view
is run and the webpage loaded, I just want to number the list of items in each table as it's created.

How would I do this? I am knew to Python Django, so any help or guidance would be much appreciated.

Edit

The part of the HTML that is currently displaying these tables in the webpage looks like this:

<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>


Edit

The HTML for the file passed into the
render_to_string(...)
view has the following structure:

{% 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>

Answer

Probaly forloop.counter is what you are looking for.

Just use it in your template like this:

<ul>
{% for data in data_list %}
    <li>{{ forloop.counter }}</li>
{% endfor %}
</ul>

As for your file hope my modifications will work (marked it with your username):

    {% 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> 
Comments