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%}
Related
I have defined one custom tag which is working fine in templates.
Like
{% get_setting "DATE_FORMAT_UI" %}
Above statement is returning correct value in template.
Now i want to use the same in a filter like this -
{{extra_info.to_date|date: '{% get_setting "DATE_FORMAT_UI" %}' }}
But this is giving error in parsing.
I tried in different ways of using quotes for the {% get_setting "DATE_FORMAT_UI" %}
But every time bad luck.
So could any body help me in solving this. I want to pass date format in filter . That date format is saved into config file. but how to pass that value dynamically in filter.
The trick is to first assign this to a variable (here myformat), and then use that variable:
{% get_setting 'DATE_FORMAT_UI' as myformat %}
{{extra_info.to_date|date:myformat }}
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
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?
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" %}
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