Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using increased/decreased variables in django templates

Maybe it's a little bit stupid question, but I didn't find an answer. Is there any way to use increased/decreased variables in django templates?

e.g.{{ some_variable + 1 }}

like image 244
mindmaster Avatar asked Aug 14 '11 17:08

mindmaster


People also ask

What is a more efficient way to pass variables from template to view in Django?

POST form (your current approach) This answer is perfect and I learned a great deal!

How do I increment a template in Django?

This template tag allows you to increment a variable within a template. This avoids the need of having to use the add filter and the syntax is quite intuitive. ''' usage {% ++ <var_name> %} For example {% ++ a %} ''' def increment_var(parser, token): parts = token.

How do I change the value of a variable in a Django template?

We can set the value of a variable in the Django template using with tag. This will output the below content. One downside of this approach is that we have to write the lines where we are accessing the variable inside with and endwith block. Using with is useful when using a costly variable multiple times.

Which characters are illegal in template variable name in Django?

Variable names consist of any combination of alphanumeric characters and the underscore ( "_" ) but may not start with an underscore, and may not be a number.


1 Answers

There's a built-in add filter:

{{ some_variable|add:"1" }}
like image 85
Daniel Roseman Avatar answered Nov 15 '22 03:11

Daniel Roseman