On Wed, Jan 20, 2016 at 6:14 AM, sonu kumar <[email protected]> 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 [email protected].
To post to this group, send email to [email protected].
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%2BciXWfjR%3DcE32wQVKtwKM8p8SDkGPKwc2bcE5dE8NmyZY2w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to