Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is boolean logic possible in django templates?

Tags:

I want to do something like:

{% if ("view_video" in video_perms) OR purchase_override %} 

Is that possible?

like image 215
willcritchlow Avatar asked Jan 17 '11 09:01

willcritchlow


People also ask

Which option does Django templates accept?

DjangoTemplates engines accept the following OPTIONS : 'autoescape' : a boolean that controls whether HTML autoescaping is enabled. It defaults to True . Only set it to False if you're rendering non-HTML templates!

What does the Django templates contain?

Django Templates. Django provides a convenient way to generate dynamic HTML pages by using its template system. A template consists of static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted.

Which characters are illegal in template variable names 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.

Can we write Python code in Django template?

You cannot use python code in django template. This is by design, Django's idea of template is to isolate the presentation logic from the programming code.


1 Answers

Django docs on boolean operators

Gives you:

{% if user in users %}   If users is a QuerySet, this will appear if user is an   instance that belongs to the QuerySet. {% endif %} 

and

{% if a == b or c == d and e %} 

Be aware that and has a higher order of precedence than or, and that parentheses are not possible. If required use nested blocks.

like image 185
Spacedman Avatar answered Oct 05 '22 12:10

Spacedman