Why counter tag is not working as expected in django?

47 views
Skip to first unread message

sonu kumar

unread,
Jan 20, 2016, 9:14:24 AM1/20/16
to Django users

https://djangosnippets.org/snippets/2619/

According to above snippet if we access {{ counter_var }} in template after initializing it should return previous count. But in my case it does not return anything.


template code

<div class="question_number pull-left">Q{% counter %}. </div>
...
<script>var no_of_questions={{ counter_var }}</script>


Template tag is registered and it prints correctly Q<count>. except counter_var. Even I tried by setting context variable counter_var from view as {'counter_var':0,...} then it returns '0'.

How to get it working so that it returns previous counter?
e.g.

Q1. 
Q2.

then counter_var should return 2 instead of nothing.

Django: 1.8.6
Python: 2.7


Note: I asked same question on stackoverflow but no help till now.


James Schneider

unread,
Jan 20, 2016, 5:03:42 PM1/20/16
to django...@googlegroups.com
On Wed, Jan 20, 2016 at 6:14 AM, sonu kumar <sonun...@gmail.com> wrote:

https://djangosnippets.org/snippets/2619/

According to above snippet if we access {{ counter_var }} in template after initializing it should return previous count. But in my case it does not return anything.


That snippet is from 2011, and is tagged for Django 1.3. I'd be careful with it. There's been a fair number of changes to template tags between 1.3 and 1.8, so I wouldn't be surprised if something is slightly off here.
 

template code

<div class="question_number pull-left">Q{% counter %}. </div>
...
<script>var no_of_questions={{ counter_var }}</script>



Have you installed the django-debug-toolbar and inspected the value of the context? It could be that the snippet is no longer updating counter_var in the context correctly. Also make sure that you are using {% counter %} and {{ counter_var }} inside the same set of {% block %} tags, as variable scope does not extend outside of blocks.
 

Template tag is registered and it prints correctly Q<count>. except counter_var. Even I tried by setting context variable counter_var from view as {'counter_var':0,...} then it returns '0'.

How to get it working so that it returns previous counter?
e.g.

Q1. 
Q2.

then counter_var should return 2 instead of nothing.

Django: 1.8.6
Python: 2.7


How exactly are you generating these questions? Is there another context variable that contains the list of questions that you are printing? And if so, are you using a {% for %} loop to generate them? If so, I would recommend you use {{ forloop.counter }} rather than trying to track the counts yourself. See the list of variables available inside of {% for %} loops at the end of this section: 

 

Note: I asked same question on stackoverflow but no help till now.

A link to the SO question would be helpful if you want help there, or if someone wants reputation points.

-James

sonu kumar

unread,
Jan 20, 2016, 11:00:57 PM1/20/16
to Django users
SO link: http://stackoverflow.com/questions/34894964/why-counter-tag-is-not-working-as-expected-in-django

I am using two for loop one is for main question and another one is for sub questions. 
Why? We have some comprehension type question, in comprehension type question there could be more than one questions so one outer for loop is running for main question and another one is sub questions.  

James Schneider

unread,
Jan 21, 2016, 5:58:21 PM1/21/16
to django...@googlegroups.com
On Wed, Jan 20, 2016 at 8:00 PM, sonu kumar <sonun...@gmail.com> wrote:
SO link: http://stackoverflow.com/questions/34894964/why-counter-tag-is-not-working-as-expected-in-django

I am using two for loop one is for main question and another one is for sub questions.  
Why? We have some comprehension type question, in comprehension type question there could be more than one questions so one outer for loop is running for main question and another one is sub questions.  

Right. You would have two {% for %} loops. If you look at the table of variables made available for those loops, you can see that you can access the counters for both the inner and outer loops separately. No need for an external template tag to keep track. If all you are doing is automatically numbering questions (and sub-questions with an inner {% for %} loop), then the {% for %} loop already provides everything you need.

{% for question in questions %}
{{ forloop.counter }}: {{ question}}
    {% for sub_question in question.sub_questions %}
        {{ forloop.counter }}: {{ sub_question }}
    {% endfor %}
{% endfor %}

Would produce something like:

1: What is your name?
    1: Is it Frodo?
    2: Is it Harry Potter?
2: What is your quest?
    1: To be The Doctor?
    2: To seek out new life and new civilizations?
    3: To join the dark side?
3: What is your favorite color?
    1: Is it blue?
    2: Is it yellow?

The only instance where I can see the tag being of use is if you are keeping track of a particular type of question that may not match all elements in the loop. Then again, you can also work around that with a proper data structure being fed to your template, and then use filters like |length to figure out how many of each type of question you have. 

-James

sonu kumar

unread,
Jan 21, 2016, 9:02:11 PM1/21/16
to django...@googlegroups.com
No, I am numbering in different ways. each question got different number event hough it's part of sub question.
Yeah I got working see my gist 

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/MxHQS3S_u7Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWvxZS4sTeLbbOqYRuFP3kSzgPiT84%3DatQpq6HH0JCfTw%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Regards:
Sonu Kumar
Reply all
Reply to author
Forward
0 new messages