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. On Thursday, January 21, 2016 at 3:33:42 AM UTC+5:30, James Schneider wrote: > > > > On Wed, Jan 20, 2016 at 6:14 AM, sonu kumar <sonun...@gmail.com > <javascript:>> 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: > > https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#for > > >> *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 > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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/65753566-d52e-4dcd-ad20-34e01ae45a4e%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.