Django `if` statement isn't working? - django

In my template, I added the following debugging statement:
<script>
console.log("leaderboard? {{ client_settings.LEADERBOARD_ENABLED }}");
</script>
On the console, I see:
[14:09:20.026] "leaderboard? false"
Later in my code, I have the following code:
{% if client_settings.LEADERBOARD_ENABLED %}
<button data-theme='a' onClick="$('.leaderboard').slideDown();">Leaderboard</button>
{% endif %}
which I would think cause the Leaderboard button not to appear... but it does! Can anyone see why this is?

The Python value for boolean false is stringified "False" with a capital F. Since your console statement has "false" with lowercase f, the value of client_settings.LEADERBOARD_ENABLED is probably the string "false", which would be interpreted as boolean True.
The Pythonic way to change this would be to use True and False when setting the LEADERBOARD_ENABLED variable, instead of the strings "true" and "false". If that is not feasible, you could change the template test to:
{% if client_settings.LEADERBOARD_ENABLED == "true" %}

Related

django template language is not the same as jinja2

i'm new in Django. i'm really curious to Django template language. i have used jinja2 before Django template language. Some people says that jinja2 and Django template language are the same. But i stuck on if statement on Django template language. usually when we are comparing some value to "True" we are usually not using "==" :
{% if somevalue %}
.....
{% endif %}
instead of....
{% if somevalue == true %}
.....
{% endif %}
i can't do the first method... why ???
Jinja templates took inspiration from (copied and extended) Django templates which is why they are similar in many ways.
The first "if" block will be rendered if somevalue is "truthy" (not False, 0, blank string, empty collection or objects class has a __bool__ method that is returning True) and the second "if" block will be rendered if somevalue is equal to True which would be when somevalue is either True or 1

Django templates - comparing variables to integer constants

Seems like elementary question, and yet can't get it work
{% if iterator.next > 10 %}
Do smth
{% endif %}
Two issues. First, this code just won't work (the code in the if-condition never implemented even when the condition seems to hold true), and second issue - the ">" sign is highlighted as if it where the closing tag of the closest open tag. Any ideas how to fix the first issue and is it all right with second issues ? Maybe there's some elegant syntax that I am missing and that would remove this ambiguity for the text editor ?
iterator.next may be a string which would result in the statement being False.
Try creating a custom filter to convert it to an int. For example create "my_filters.py":
# templatetags/my_filters.py
from django import template
register = template.Library()
#register.filter()
def to_int(value):
return int(value)
Then in your template:
{% load my_filters %}
{% if iterator.next|to_int > 10 %}
Do smth
{% endif %}
More on custom tags and filters here
I wouldn't worry about the highlighting, this may just be your IDE. I recommend using PyCharm for Django development
Django's docs says that you can use > with if tag:
{% if somevar < 100 %}
This appears if variable somevar is less than 100.
{% endif %}
take a look at documentation: https://docs.djangoproject.com/en/1.9/ref/templates/builtins/
maybe you are missing something else?

django and condition in ifequal statement throws error

I need to display a portion of HTML on the following condition,
var1=="google" and var2 is True
I wrote the following code ,
{% ifequal var1 "google" and var2 %}
/*HTML CODE */
{% endif %}
and i got an error
TemplateSyntaxError at /process/apply.html
u'ifequal' takes two arguments
I know i can split the above two nested IF statements,still is there a way in django to combine them to a single if statement?
From django ifequal documentation
It is only possible to compare an argument to template variables or strings. You cannot check for equality with Python objects such as True or False. If you need to test if something is true or false, use the if tag instead.
So if you want to check for True or False, so need to use if.
{%if var1 == "google" and var2 %}
....
{%endif%}

django tags and html buttons

In my base.html I have:
blabla
{% ifequal alterprofile no %}
{% include 'registration/submittedprofile.html' %}
{% else %}
{% include 'registration/submittedprofile2.html' %}
{% endifnotequal %}
blabla
In views.py I have alterprofile = "no".
How do i change alterprofile to "yes". This is my submittedprofile:
<form action="" method="get">
blablabla
<input type="submit" value="Make Changes">
</form>
And this is my views.py:
def userprofile(request):
alterprofile = "no"
username = request.user
return render_to_response('registration/userprofile.html', {'user': username, 'alterprofile' = alterprofile})
Is there anyone who can code the answers for me. I've tried playing round with the previous answers but to no affect.
Django variables are rendered from the server side, so you can not change the variable after it was passed to your template. What you want to achieve is done via frontend scripting.
In this case you would pass both variables to the django template, save them in your Javascript and then switch them once you clicked the button you mentioned (via onClick event handling).
You can use url arguments like:
/myurl/
/myurl/?show2
then, in your views.py you can use request.POST['show2'] to check if exists and then send a variable again to the view to be checked with your {if}s
As an aside note, either you don't understand basic request flow with web applications or you are not explaining properly what you mean with "html button", so you are not fluent with html language. Sorry if my intuition is harsh or wrong.
its not that clear what you are asking but here is how to make the logic work as I think you want it based on yoru submitted code:
in your template change it to
ifequal alterprofile "no"
to include registration/submittedprofile.html.
When you change the view to alterprofile = "yes" the registration/submittedprofile2.html will be included instead if you keep your current template logic.
This is because in your view, alterprofile is assigned a string therefore its always a string. When you tried to test against no instead of "no" django was looking for a variable called no which doesn't exist.
This means that everytime you run it would have always included registration/submittedprofile2.html

django template filter

I have this code situation
{% if 1|floatformat in '7,10' %}
yes
{% else %}
no
{% endif %}
the return result is always set to "yes", how to make the result return "no"
please help.
thanks
It's hard to make out from your example what exactly is happening here, because you're substituting dummy data for what's really coming out of the database.
As you've written it, the result will always be "yes" because the string '7,10' contains the string '1'.
It sounds like what you're trying to achieve is:
If this number is in this list, then yes, otherwise no.
So let's rewrite this template to be a little more real:
{% if mynumber in yeslist %} yes {% else %} no {% endif %}
This assumes that:
mynumber is a number
yeslist is a list of numbers
I'm not sure what you're using floatformat for in this case.
If the above assertions aren't true, and you have to use strings, then your work is much harder, and you should be processing yeslist server-side. For example, if yeslist is just a string like "7,10,123,93,9,19,83", then figuring out if the number 8 is in the list will be needlessly difficult in templates. Much easier to do it in your view:
def myview(request):
ctx = {}
# ... do some work ...
# yeslist now has a string like "7,10,123,93,9,19,83"
ctx['yeslist'] = yeslist.split(',')
# ... do more work, and render the response ...
Now, {% if '8' in yeslist %} will no longer return a false positive, because it's not doing a substring match, it's doing list membership.
1|floatformat returns 1 and the condition checks if 1 is in '7,10', since django considers '7,10' as a string it returns True. Try passing a list and it will return no