36👍
Changing the state of an object in a Django template is discouraged. You should probably bite the bullet, calculate the condition beforehand and pass extra state to the template so you can simplify the template logic.
I’m no purist in this regard by the way, but I have been bitten by the purposeful limitations of Django templates a few times. You’re better off not fighting against it, in my opinion.
Being that your intention seems to be to filter out non-matching items, an alternative would be to filter out those in the view and then use {{ forloop.counter }}
to sort out the link text you want. So in the view you have something like this:
new_lst = filter(lambda x: x.attr0 == attr0 and x.attr1 == attr1, lst)
And then, in your template:
{% for object in new_lst %}
<li><a href="{{ object.get_absolute_url }}"> Link {{ forloop.counter }} </a></li>
{% endfor %}
16👍
Although this has been answered and goes against what has been said I just had a thought and can’t see too much harm if perhaps you made simple counter class like
class Counter:
count = 0
def increment(self):
self.count += 1
return ''
def decrement(self):
self.count -= 1
return ''
def double(self):
self.count *= 2
return ''
then in your template {{ counter.increment }}
{{ counter.count }}
etc.
- [Django]-Django and Middleware which uses request.user is always Anonymous
- [Django]-Django best approach for creating multiple type users
- [Django]-Track the number of "page views" or "hits" of an object?
5👍
For increase value in template django:
if varint=6, the result will be 8
{{ varint|add:"2" }}
For decrease value in template django:
if varint=6, the result will be 5
{{ varint|add:"-1" }}
- [Django]-Add Indexes (db_index=True)
- [Django]-Django: Rest Framework authenticate header
- [Django]-Convert an IP string to a number and vice versa
0👍
I have just found out the answer myself. As I said I was using this custom tag, which assigns a value to a variable. What is actually does is set give a value to a variable in the context
so I just retrieved the value from the context and incremented it.
Here is the code
class IncrementVarNode(template.Node):
def __init__(self, var_name):
self.var_name = var_name
def render(self,context):
value = context[self.var_name]
context[self.var_name] = value + 1
return u""
def increment_var(parser, token):
parts = token.split_contents()
if len(parts) < 2:
raise template.TemplateSyntaxError("'increment' tag must be of the form: {% increment <var_name> %}")
return IncrementVarNode(parts[1])
register.tag('increment', increment_var)
It can be used like this {% increment <var_name> %}
but this var_name should have been set to a value previously using the custom tag mentioned above as {% set <var_name> = <var_value> %}
- [Django]-How to test custom django-admin commands
- [Django]-Get type of Django form widget from within template
- [Django]-Django bulk_create function example