I have this template (set_ip.j2):
{% if '{{ansible_env.SSH_CONNECTION.split(' ')[2]}}' == '{{ ip_ssh }}' %}
address = {{ ip_db }}
name='db1'
{% endif %}
but this condition not work! I want address and name set by this value in the config file.
Never ever use nested expressions in Jinja2:
{% if ansible_env.SSH_CONNECTION.split(' ')[2] == ip_ssh %}
address = {{ ip_db }}
name='db1'
{% endif %}
Related
In a django - webapp I am classifying between two classes of images i.e. Ants and Bees
I have returned the dictionary to the templates(index.html)
context={
'ant':("%.2f" % predictions[0]),
'bee': ("%.2f" % predictions[1]),
}
when applying this
{% for key , value in pred.items %}
<p>{{key}}: {{value}}%</p>
{% endfor %}
i got this which is pretty much what i wanted to display now i want to display the one with greater probability how do i do it ?
I cannot access elements of the dictionary inside if else statement , though i tried doing this
{% for key, value in pred.items %}
{% if value.0 > value.1 %}
<p>Result : {{value.0}}</p>
{% elif value.0 < value.1 %}
<p>Result: {{key}}</p>
{% endif %}
{% endfor %}
Since your data structure does not look very dynamic and flexible, you could do it the following static way:
Result:
{% if pred.ant > pred.bee %}
Ant: {{ pred.ant }}
{% elif pred.ant < pred.bee %}
Bee: {{ pred.bee }}
{% else %}
Ant: {{ pred.ant }}
Bee: {{ pred.bee }}
{% endif %}
I have a for loop in Django template. After that, I check for coincidences. But in some cases, there are might be 3 coincidences. I need to show only the first coincidence. Now, my code returns the name for 3 times, because, there are 3 coincidences
{% for ip in ips %}
{% if d.name == ip.name %}
<strong>{{ d.name}} </strong>
{% endif %}
{% endfor %}
SOLUTION
It is impossible to break forloop in django template, so I decided to change in views.py through queryset distinction of similar names
ips = Point.objects.defer('point').order_by('name').distinct('name')
I don't recommend doing this in Django Template , but in views itself. But if you can't then you can use {{ forloop|break }}.
Something like this :
{% for ip in ips %}
{% if d.name == ip.name %}
{{ forloop|break }}
<strong>{{ d.name}} </strong>
{% endif %}
{% endfor %}
Check the small snippet example here...
I'm using this code to set variable and then check if this variable is 1:
{% if pillar['setup_user'] is defined %}
{% set var_setup_user = pillar['setup_user'] %}
{% else %}
{% set var_setup_user = 1 %}
{% endif %}
{% if var_setup_user == 1 %}
setup-user:
cmd.run:
- name: |
...
- shell: /bin/bash
- user: root
{% endif %}
Is there a way to check this in one or maybe two lines?
Thank you
{% set var_setup_user = pillar['setup_user']|d(1) %}
d is the alias for default filter.
Does a normal ternary operator work?
{% set var_setup_user = pillar['setup_user'] if pillar['setup_user'] is defined else 1 %}
I have a condition within a loop in my template like this:
{% for message in message_trash %}
<td><a href="#">
{% if request.session.user_email == message.message_user_reciever.user_email %}
{{ message.message_user_reciever.user_firstName }} {{ message.message_user_reciever.user_lastName }}
{% elif request.session.user_email == message.message_user_sender.user_email %}
{{ message.message_user_sender.user_firstName }} {{ message.message_user_sender.user_lastName }}
{% endif %}
</a><small>Friends</small></td>
{% endfor %}
but i don't know why i get this error when applying the url?
TemplateSyntaxError: Could not parse the remainder: '==message.message_user_reciever.user_email' from 'request.session.user_email==message.message_user_reciever.user_email'
Update:
this is the view and variables that i render to the template:
def trashMessages(request, userId):
if isMessageOwner(request, userId):
user = Users.objects.get(user_id=userId)
message_trash = Messages.objects.filter(Q(message_user_reciever= user, message_sender_type='TRASH') | Q(message_user_sender=user, message_reciever_type='TRASH'))
return render(request, 'navigation_messages.html', {'user': user, 'message_trash': message_trash, 'type': 'trash'})
On testing your code out, I can only replicate your issue is by swapping:
{% if request.session.user_email == message.message_user_reciever.user_email %}
for
{% if request.session.user_email ==message.message_user_reciever.user_email %}
Note the missing space. Is the snippet in your question exactly as it is in your template?
I have some django code that prints a BooleanField
it is rendered as True or False, can I change the label to be Agree/Disagree or do I need to write logic for that in the template?
{{ bool_var|yesno:"Agree,Disagree" }}
You can also provide an additional string for the None case. See the docs for yesno for details.
Just another way if you want to have more options like adding HTML elements and classes
{% if var == True %} Yes {% else %} No {% endif %}
You can change Yes and No to any html element; an image or span element
Any of the following may be tried with consistent results:
A.
{% if form.my_bool.value %}
{{ "Yes" }}
{% else %}
{{ "No" }}
{% endif %}
B.
{{ form.my_bool.value|yesno }}
C.
{{ form.my_bool.value|yesno:"Yes,No" }}
D.
{% if form.my_bool.value == True %} Yes {% else %} No {% endif %}
Or simply,
{{ form.my_bool.value }} # Here the output will be True or False, as the case may be.
If your models have been defined as
class mymodel(models.Model):
choices=((True, 'Agree'), (False,'Disagree'),(None,"Maybe"))
attr = models.BooleanField(choices=choices, blank=False, null=True)
You can use the built-in method to retrieve the "pretty" string associated with the value by in your template with
{{ object.get_attr_display }}