Check query field value in Django template - django-templates

I am passing a query to the context of a normal Django template. Within the template I want to do this:
{% for item in sample %}
{% if item.status != "Close" %}
Show something
{% endif %}
{% endfor %}
However, the if condition is never evaluated to True.
I can perfectly access the field for each item in the template like {{item.status}} but the if part is not working.
Is there a special way to write the if statement so the template expands item.status to its value before evaluating the comparison?

Related

Jinja2 boolean condition not checking out as expected

I am trying to understand how Jinja2 passes variables from my flask app with flask- login to a jinja2 template and how it interacts with the User class variable is_authenticated. Currently in my app this conditional statement seem to fully work as intented:
{% if current_user.is_authenticated %}
<h1>Hi {{ current_user.username }}!</h1>
{% endif %}
But if I try to be more explicit in my jinja2 statement for once, nothing seem to work out, among others that I have tried:
{% if current_user.is_authenticated is sameas true %}
{% if current_user.is_authenticated == "True" %}
{% if current_user.is_authenticated == true %}
{% if current_user.is_authenticated == "1" %}
...plus a whole battery of things that seem not to be working. When I print out the variable user.is_authenticated is is:
<bound method User.is_authenticated of <User 1>>
How can I explicitly check this one for being true? Why is jinja2 accepting {% if current_user.is_authenticated %} but not a more explicit statement like {% if current_user.is_authenticated == true %} ? What am I missing?

Shopify - Change display when switching the variants (liquid)

I would like to add a new function to the product details page that shows me the current quantity of the variant that is in stock.
I get this far with Liquid, only if another variant is selected, the display of the amount does not change,
Anyone of you have any ideas how I can do that?
This shows me the current variant, but does not change when I change the selection.
{% - for variant in product.variants -%}
{{current_variant.inventory_quantity}}
{% - endfor -%}
You can't use Liquid for this. Liquid is just Shopify's templating language and is only useful for the initial page render.
I know how to do this. You need to use vue so it‘s reactive on the front end, also each product with variants now has to be a collection. You will need to loop through the products in the collection and get the linked products with javascript.
So let‘s say you have a specific product and you have three versions of it, you would actually create three separate products.
The next thing to do is link them with liquid and javascript, so in your product.liquid file open a set of script tags and you would start it like this
{% assign current_product = product %}
const product = (function() {
const product = {{ product | json }};
return product
})();
const linked_products = (function() {
const linked_products = []
{% for collection in current_product.collections %}
{% for product in collection.products %}
{% if product.id != current_product.id and product.title == current_product.title %}
product = {{ product | json }}
linked_products.push(product);
{% endif %}
{% endfor %}
{% endfor %}
return linked_products;
})();
This is just to get the ball rolling but you will need to handle quite a bit of other things and this is quite a large project.

how to parse this python code django template language

for com in applications:
for number in range(len(appliers)):
if connector[number] == com.id:
print(appliers[number].name)
I am having problem with parsing this python code to django template language... keep getting error saying that it can't parse. please can anyone parse it for me...
You can create a filter for getting a range in template:
#register.filter(name='times')
def times(number):
return range(number)
And then in template you can do:
{% for number in appliers|length|times %}
{% if connector.number == commission.id %}
{{appliers.number.name}}
{% endif %}
{% endfor %}
Alternatively you can pass range(len(appliers)) as a context variable to your template from a view.

check variable type inside Jinja2 in Flask

The template file i created contains this:
{% if type({'a':1,'b':2}) is dict %}
print "Oh Yes!!"
{% else %}
print "Oh No!!!"
{% endif %}
Then Jinja2 responds by saying:
TemplateAssertionError: no test named 'dict'
I am completely new to Jinja2 and Flask
You are looking for the mapping test:
{% if {'a': 1, 'b': 2} is mapping %}
"Oh Yes!"
{% else %}
"Oh No!"
{% endif %}
Jinja is not Python though, so you don't have access to all the builtins (type and print do not exist, for example, unless you add them to the context. In Flask, you do this with the context_processor decorator).
You don't actually need print at all. By default everything is output (unless you are in a child template that extends a parent, in which case you can do interesting things like the NULL Master fallback because only blocks with names available in the master template are output).
How about:
{% if {'a':1,'b':2} is mapping %}
print "Oh Yes!!"
{% else %}
print "Oh No!!!"
{% endif %}
see List of Builtin Tests for reference.
In case you want to get a custom type you can access field name like in this example:
{% if 'RelationField' in field.__class__.__name__ %}
<div class="col-md-1">
Manage object
</div>
{% endif %}

How can I access template variable in TWIG macro?

I'm not able to access template variable in TWIG macro.
Here is a simplified example:
{% set myname = "Ligio" %}
{{ _self.pagedurl(1) }}
{% macro pagedurl(page) %}
Hi {{ _self.myname }}! This is Page Num {{ page }}
{% endmacro %}
How can I access the variable myname without passing it to the macro?
You can not.
As stated in the documentation:
As PHP functions, macros don't have access to the current template
variables.
Your only solution is to pass the parameter to the macro:
{% import _self as flow %}
{{ flow.pagedurl(1, "Ligio") }}
{% macro pagedurl(page, myname) %}
Hi {{ myname }}! This is Page Num {{ page }}
{% endmacro %}
IMPORTANT NOTE:
You may have noticed in my example, I call {% import _self as flow %}.
This is something you MUST do:
When you define a macro in the template where you are going to use it,
you might be tempted to call the macro directly via _self.input()
instead of importing it; even if seems to work, this is just a
side-effect of the current implementation and it won't work anymore in
Twig 2.x.
http://twig.sensiolabs.org/doc/tags/macro.html
If you need to pass more than one global variable into the macro, you might find the _context variable useful:
{% macro mymacro(globalvars) %}
Value of the global variable pi is {{ globalvars.pi }}
{% endmacro %}
{% set pi = 3.14159 %}
{{ _self.mymacro(_context) }}
Ref: this or this answer.
You can set a global variable and access it anywhere in the template
$loader = new \Twig_Loader_Filesystem('path/to/templates');
$twig = new \Twig_Environment($loader);
$twig->addGlobal('V_Name', 'V_Value');