Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

forloop.counter not render in Django1.10 for TemplateColumn #488

Closed
djk2 opened this issue Oct 17, 2017 · 10 comments · Fixed by Karaage-Cluster/karaage#354, mozilla/addons-server#6790, rlmv/doc-trips#97 or drummonds/bene#50

Comments

@djk2
Copy link
Contributor

djk2 commented Oct 17, 2017

django-tables2==1.0.4

class SomeTable(tables.Table):
    action = TemplateColumn("{{ forloop.counter }}")

for Django 1.9.13 it works fine.
for Django 1.10 forloop not exist in table.context and not rendered correctly

Have you any idea?
I spent on this problem 8 hours trying solve it.

@jieter
Copy link
Owner

jieter commented Oct 17, 2017

so with the exact same version of django-tables2, different django versions show this behaviour?

@djk2
Copy link
Contributor Author

djk2 commented Oct 18, 2017

Exactly, different django versions show this behaviour.

@jieter
Copy link
Owner

jieter commented Oct 18, 2017

can you maybe supply a simple test case?

@djk2
Copy link
Contributor Author

djk2 commented Oct 18, 2017

I will prepare unit test today evening.

@djk2
Copy link
Contributor Author

djk2 commented Oct 23, 2017

For current master I can't reproduce this error.
Templatetag forloop.counter is never render in TemplateColumn.
I tested for all supported Django versions and current DjT2 can't render forloop. In past that feature works fine.
This was very helpfull for generate css ids for JS.

djk2 added a commit to djk2/django-tables2 that referenced this issue Oct 23, 2017
@jieter
Copy link
Owner

jieter commented Oct 24, 2017

I think it makes sense to add a context variable containing a counter without counting on the template context leaking through... What do you think?

If you have time, feel free to open a PR.

@jieter
Copy link
Owner

jieter commented Oct 30, 2017

As of version 1.14.2, a template variable row_counter is available.

class CountryTable(tables.Table):
        counter = tables.TemplateColumn('{{ row_counter }}', verbose_name='#')

1.14.2 is released.

@waketzheng
Copy link

Now that row_counter is start from 0, if I want to start from 1, how to do it?

@jieter
Copy link
Owner

jieter commented May 1, 2018

You can use the add template filter: https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#add

@djk2
Copy link
Contributor Author

djk2 commented May 2, 2018

class CountryTable(tables.Table):
        counter = tables.TemplateColumn('{{ row_counter|add:1 }}', verbose_name='#')

# for free to join this conversation on GitHub. Already have an account? # to comment