django template language is not the same as jinja2 - django

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

Related

Why is widthratio (multiplication) not working in my template?

I'm using Django 2.0 and Python 3.7. I've read I can do multiplication in templates by using widthratio -- multiplication in django template without using manually created template tag . However, in a template of mine, I'm trying this to no avail. I'm trying
{% if widthratio articlestat.score 1 TRENDING_PCT_FLOOR >= articlestat.weighted_score %}style="font-weight:bold;"{% endif %}
When my template is executed with this code, it gives the error ...
Unused 'articlestat.score' at end of if expression.
I want my if expression to say if the multiple of "articlestat.score" and "TRENDING_PCT_FLOOR" is greater than "articlestat.weighted_score," print this out, but I can't seem to figure out how to do that.
You can't use template tags inside if statement conditionals like that. What you can do is first assign the output of widthratio to a template variable, and then compare that in your if statement:
{% widthratio articlestat.score 1 TRENDING_PCT_FLOOR as ratio %}
{% if ratio >= articlestat.weighted_score %}style="font-weight:bold;"{% endif %}
I have the habit of using template tags only for pure templating questions (e.g. formating a number to dollars) and leaving all logic to the models (if the logic is model-specific) or views (if it's a business logic or depends on what view you're in).
Instead of using a custom template tag, would add a property to the articlestat model when TRENDING_PCT_FLOOR is static:
class ArticleStat(models.Model):
TRENDING_PCT_FLOOR = x
#property
def is_ratio_positive(self):
ratio = self.score * self.TRENDING_PCT_FLOOR
return ratio >= self.weighted_score
Then on the template, I would use:
{% if articlestat.is_ratio_positive %}style="font-weight:bold;"{% endif %}
If articlestat is not a model (e.g. it was created on views.py) then I would use the logic above on the corresponding view.

django template tag on multiple line

I am creating a custom django template tag by using such a code :
#register.simple_tag(takes_context=True)
def render_listing(context, *args, **kwargs):
... my code ...
This works well, but in my template, it seems that all parameters must be on a single line, for example :
this works:
{% render_listing param1=val1 param2=val2 ... paramN=valN %}
but on multiple lines, it does not work :
{% render_listing param1=val1
param2=val2
...
paramN=valN %}
I tried multiple escape sequences but I did not succeeded,
Is there a way to specify a template tag on multiple lines ?
No, the Django template language does not support multiple line tags. See ticket 8652, which was closed as WONTFIX, or this thread from the django-developers mailing list.
Sometimes, if there is a repeated prefix, you can make it more readable by using the with tag. For example if you have,
{% render_listing param1=long.common.prefix.val1 param2=long.common.prefix.val2 param2=long.common.prefix.val3 %}
you could rewrite as
{% with prefix=long.common.prefix %}
{% render_listing param1=prefix.val1 param2=prefix.val2 param2=prefix.val3 %}
{% endwith %}
Often (but not always), a really long tag is an indication that you're putting too much logic in the template. See if you can move some of it into the view, model method, template tag or template filter.
It's pretty straightforward to enable, though hackish:
import re
from django.template import base
base.tag_re = re.compile(base.tag_re.pattern, re.DOTALL)
"Using" it is simple; one place I find it especially useful is {% include %} tags:
{% include 'my/sweet/modal-template.template' with
message="Hey, do you really want to frob the widget?"
yes="Heck yes I do!"
no="No frickin' way!"
icon="error"
%}
I haven't tested this in more recent versions of Django but I imagine it could be adapted; that worked at least back around 1.8. I should point out that in theory some tags that do custom parsing of their arguments could break; in practice I haven't had any trouble in the last ~10 years of Django programming.

what is difference between {{}} and {% %} in django templates

I am very new to django and working on it.. I visited a html file and dont know the difference between {{}} and {% %} in html files used
as here
{% load static %}
Thanks a lot
You can use
{% %} For sentences such as if and for or to call tags such as load, static, etc.
{{ }} To render variables in the template.
Read More about it at Django Docs
{% %} is for displaying code and {{}} is for displaying variables
There are three things in the template in Django
First is template variable and the second thing is template tag and third and last is template filter
so we write a template variable is {{}}
and write a template tag is {% %}
third and last is template filter {{variable |filter:arg}}
I'm new too for Django, so if i'm wrong, please someone correct me.
The difference between they are:
{{variable}} is used to use a variables. When the template encounters a variable, it evaluates that variable and replaces it with the result.
You also can use filters {{variable|filter}} like this:
{{name|length}} in this case you will use a variable "name" and return the length of that variable.
{%tag%} could use for loops or logic, or load external information into the template to be used by later variables. You can create block tags to help extend other html files parts. Also you can create custom tags.
A good place to see how to do it:
https://www.codementor.io/hiteshgarg14/creating-custom-template-tags-in-django-application-58wvmqm5f
Tags like loops and block, need to be closed.
{% %} for IF ELSE CONDITIONS and FOR LOOP etc
{{ }} for veriables that rendered from view function also used in FOR LOOP veriables like
`enter code here`
{% for obj in qs%}
{{ obj.veriable_name }}
{% endfor %}

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?

Extending base.html conditionally, in Django template

Based upon whether a custom-defined context variable {{ state }} is true or false, I want to extend a different flavor of base.html in a Django template.
What's the Django template code to make this happen?
I'm trying: {% extends state|yesno:"base1.html;base2.html" %} but unsure whether this is correct.
You are almost there - you just need to replace your semi-colon ; with a commma ,:
{% extends state|yesno:"base1.html,base2.html" %}