why is this django template elif not working - django

I can't figure out why this simple if ... else ... endif is not working. The two variables being compared in the if are equal in the page output so the if should not trigger but it does. Any help is appreciated.
{% if view.get_player_id != user.id %}
<h1>'{{ view.get_player_id }}' '{{ user.id }}' Only {{ view.get_player_name }} can edit this page</h1>
{% else %}
...
{% endif %}
And this is the page output:
'4' '4' Only Leo Messi can edit this page

Related

Unable to correctly parse URLs using Django template language

I currently am trying to link a researcher's name if they provided a link, otherwise if they provide a blank input or N/A, then I choose not to link their name. I use the information from the researcher object using Django's template language, but when I check the result it ends up linking all of them, and those without input just have href = "" which links back to the homepage.
I've tried using {{r.website_link|length}} instead, but that just creates errors and the page won't load. I'm a newbie to frontend so I'm not sure if my "if" statement is incorrect or if my HTML logic is incorrect.
<div>
{% for r in researcher %}
<div class="researcher">
{% if "{{r.website_link}}" != "N/A" and "{{r.website_link}}" != "" %}
<p><a class="researcherwebname" href="{{ r.website_link }}">{{ r.fullname|title}}</a> | {{ r.institution }} | {{r.position}} | <i>{{ r.des|capfirst }}</i></p>
{% else %}
<p> {{ r.fullname|title}} | {{ r.institution }} | {{r.position}} | <i>{{ r.des|capfirst }}</i></p>
{% endif %}
</div>
{% endfor %}
</div>
I expect it to link researchers with an actual r.website_link input, otherwise the name should be unlinked.
please try
{% if r.website_link %}
{% if r.website_link != "N/A" %}
{# display researcher #}
{% endif %}
{% endif %}

condition template in ansible

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 %}

Could not parse the remainder django template

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?

Django TemplateSyntax: Two tests with an 'If .. or...'

I am trying to run two tests:
{% if profile.id != 100 or profile.location == NULL %}
however it seems to not work. I couldn't find in the docs why this isn't working. Any ideas?
EDIT:
In the SQL db, the value for profile.location is NULL. The rendered result is None.
EDIT 2:
Here is the full chain. There are 4 ways to grab a user's location. It is a giant mess as you can see...
{% if profile.city.id != 104 or profile.city %}
{{profile.city}}
{% else %}
{% if profile.mylocation != '' %}
{{ profile.mylocation }}
{% else %}
{% if profile.fbconnect == 1 and profile.location != '' and profile.location != "None" %}
{{profile.location}}
{% else %}
{% if profile.sglocation.city %}{{profile.sglocation.city}}{% else %}{{profile.sglocation.country}}{% endif %}
{% endif %}
{% endif %}
{% endif %}
There's no NULL. Try:
{% if profile.id != 100 or not profile.location %}
If I were you, I would throw all that logic back into Python, for example:
class Profile(models.Model):
[your model fields and stuff]
def get_location(self):
[your location logic]
Then in the template you can just do:
{{ profile.get_location }}

How to know if a checkbox is checked having just the {{ form.checkbox }} form-tag

How can I know if a checkbox is checked (True, 1) having just the {{ form.checkbox }} form-tag?
'activo' is defined as (True, 1) in the db.
My template is:
{{ form.activo }}
RESULTS:
<input id="id_activo" type="checkbox" name="activo" checked="checked"/>
{{ form.activo.data }}
RESULTS:
False
{{ form.activo.value }}
RESULTS:
""
Yet no 1's or True's are coming through. :S
Any hint is appreciated. =')
It is checked if request.POST.has_key('activo') or the {{ form.activo.data }} actually returns True when initialized with request.POST.
Your question isn't quite clear, but maybe your problem has something to do with the fact, that browsers don't put anything in the POST data for an unchecked checkbox.
This made things complicated for me when I had to differentiate between a checkbox not being displayed at all and a displayed checkbox not having been checked. Just from looking at the POST data you can't tell those two cases apart.
Following on from your reply to mbarkhau's answer, using instance= doesn't make the form bound, it just provides default values to the form.
Here's the logic in a template:
{% if form.is_bound %}
{% if form.initial.activo %}
Checked.
{% else %}
Not checked.
{% endif %}
{% else %}
{% if form.activo.data %}
Checked.
{% else %}
Not checked
{% endif %}
{% endif %}
But it makes more sense to do this logic in the view and pass extra context. Something like:
context_data = {...}
if form.is_bound:
activo = form.data.get('activo')
else:
activo = form.initial.get('activo')
context_data['activo'] = bool(activo)
return render_to_response('your_template.html', context_data)
If you want a boolean condition in templates this should work:
{% if form.activo %}
--
{% else %}
---
{% endif %}