Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Django cycle template tag to number rows

I am trying to use Django cycle to make my css class have the current table row number in it. I'm trying the following:

{{ formset.management_form }}
        {% for form in formset %}
            <tr class="{% cycle 'row1' 'row2' 'row3' %} formset_row">
                {% for field in form.visible_fields %}
                       <td>
                           {# Include the hidden fields in the form #}
                           {% if forloop.first %}
                               {% for hidden in form.hidden_fields %}
                                   {{ hidden }}
                               {% endfor %}
                           {% endif %}
                           {{ field.errors.as_ul }}
                           {{ field }}
                       </td>
                   {% endfor %}
            </tr>
        {% endfor %}

Where my rows are added dynamically using jquery.formset.js https://gist.github.com/vandorjw/f884f0d51db3e7caaecd

For some reason this just gives me

<tr class="row1 formset_row">...</tr>
<tr class="row1 formset_row">...</tr>
<tr class="row1 formset_row">...</tr> 
<tr class="row1 formset_row">...</tr>
...
  1. Why isn't this working?

  2. From what I understand this will give me

row1 row2 row3 row1 row2 row3 row1...

How can I make this continue counting...

row1 row2 row3 row4 row5 row6 row7...

like image 876
Ruth Young Avatar asked Oct 22 '25 15:10

Ruth Young


1 Answers

I'm not sure why it's not working, but if you want it to continue counting along with the entire loop, you can use

class="row{{forloop.counter}}"
like image 71
NS0 Avatar answered Oct 25 '25 21:10

NS0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!