How to do an if statement in a Django template to find if a decimal value is 0 - django

Basically, I want to find out if a decimal field value is 0.00. Then, I want to output a different value in the template. See code below. The code does not work.
variable = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
#template
{% for item in table %}
{% if item.variable is 0.00 %}
<li><strong>Total: </strong> Unknown </li>
{% else %}
<li><strong>Total:</strong> ${{ item.variable }}</li>
{% endif %}
{% endfor %}
This does not work. It outputs: Total: $0.00.

You can check this with if not item.variable for example, since 0.00 has truthiness False. This condition will also check if the value is None (so NULL in the database), which is probably something you should consider as well:
{% for item in table %}
{% if not item.variable %}
<li><strong>Total: </strong> Unknown </li>
{% else %}
<li><strong>Total:</strong> ${{ item.variable }}</li>
{% endif %}
{% endfor %}
If you only want to check for zero, you can check it with == 0:
{% for item in table %}
{% if item.variable == 0 %}
<li><strong>Total: </strong> Unknown </li>
{% else %}
<li><strong>Total:</strong> ${{ item.variable }}</li>
{% endif %}
{% endfor %}

Related

Get the first element in the for loops in the Django template

Template:
{% for code in group_codes %}
*_{{ code.build }}_*<br />
{% if test_info.test_type = 0 %}
{{ code.pre_testing_fail }}/{{ code.pre_testing_total }} failed pre-test<br />
{% else %}
{% for shelf in final_shelf_info %}
{{ shelf.build }} <br/>
{% if shelf.build = code.build %}
{{ mr_script_count_func }}/{{ code.script_total }}
<span>MR</span> failed during script<br />
{{gw_script_count_func}}/{{ code.script_total }}
<span>GW</span> failed during script<br />
{{ mr_post_count_func }}/{{ code.post_testing_total }}
MR failed during post-test<br/>
{{ gw_post_count_func }}/{{ code.post_testing_total }}
GW failed during post-test<br/>
{% endif %}
{% endfor %}
<br/>
<br/>
{% endif %}
{% endfor %}
View
def final_shelf_info(self):
shelves = self.bugs_stbs()
shelfList = list()
for shelf in shelves:
shelfList.append(shelf.results_stb_id)
final_info = ResultsStbs.objects.select_related(
'build',
'pre_testing_result',
'script_result',
'post_result',
).filter(
results_stb_id__in=shelfList,
tr_test_case_id=self.kwargs['trTestCaseID'],
).order_by(
'pair_no','shelf_no',
)
for info in final_info:
if info.stb_hw_info_ids:
info.stb_type = info.stb_hw_info_ids.stb_hw_info.stb_type
else:
info.stb_type = None
return final_info
I would like to get the first element in the for loop
{% for shelf in final_shelf_info %}
and compare with another data.
How can I get the first element in the first loop.
First element : Q004.01.55.01.55.19_9423
{{ shelf[0].build }} I tried like that, it did not work.
The output of the for loop:
1234.xx.xx.xx.xx.xx
Any helps would be appreciated.
{% for shelf in final_shelf_info %}
{% if forloop.first %}
Do something with {{ shelf }} since its the first item iterated
{% endif %}
{% endfor %}
More on the {% for %} template loop in the docs.
You could do something like this:
{% for t in things %}
{% if forloop.first %}
// do something
{% endif %}
// do stuff
{% if forloop.last or things.count == 1 %}
// do something
{% endif %}
{% endfor %}
More documentation is available at Django documentation
{% if final_shelf_info.0 == shelf %}
or
{% if final_shelf_info.first == shelf %}

django blocktrans and with tag - DRY approach

How do I create a DRY approach with the following code segment?
This may be an easy question, but it has me stumped.
I have tried for the past two days, but I cannot figure out how to have just the one blocktrans while incorporating the django with tag.
Here is my template tag code:
{% if user.userprofile.subscription_category == subscription_type_free %}
{% with temp_max_language_versions=max_language_versions_free_subscription %}
{% blocktrans %}You have the ability to create <strong>{{ temp_max_language_versions }}</strong> different {{ resume_details_tran }} with your subscription.{% endblocktrans %}
{% endwith %}
{% elif user.userprofile.subscription_category == subscription_type_03 %}
{% with temp_max_language_versions=max_language_versions_level03_subscription %}
{% blocktrans %}You have the ability to create <strong>{{ temp_max_language_versions }}</strong> different {{ resume_details_tran }} with your subscription.{% endblocktrans %}
{% endwith %}
{% elif user.userprofile.subscription_category == subscription_type_06 %}
{% with temp_max_language_versions=max_language_versions_level06_subscription %}
{% blocktrans %}You have the ability to create <strong>{{ temp_max_language_versions }}</strong> different {{ resume_details_tran }} with your subscription.{% endblocktrans %}
{% endwith %}
{% elif user.userprofile.subscription_category == subscription_type_12 %}
{% with temp_max_language_versions=max_language_versions_level12_subscription %}
{% blocktrans %}You have the ability to create <strong>{{ temp_max_language_versions }}</strong> different {{ resume_details_tran }} with your subscription.{% endblocktrans %}
{% endwith %}
{% endif %}
Your HTML is always the same with a different temp_max_language_versions variable. Three ways around this:
1 - use an include:
{% if user.userprofile.subscription_category == subscription_type_free %}
{% include 'path_to_include.html' with temp_max_language_versions=max_language_versions_free_subscription %}
{% elif user.userprofile.subscription_category == subscription_type_03 %}
....
2 - OR you could add that to the category itself; some pseudo-code:
class SubscriptionCategory(...):
#property
def temp_max_language_versions(self):
if self.type == 'something':
return 'something_else'
3 - if it's specific to the view, you could add that to the category via the view as well.

Django if statement not correct

I'm not sure where I'm going wrong with this. I have a loop that's printing out a couple of email addresses. I want to set one as the "primary" address based on it's status of primary or not. If I give the template {{ x.primary }} I get the values of True or False.
I'd like my output to look like:
Work: bob#example.com (Primary)
Personal: bob2#example.com
Personal: bob3#example.com
<ul>
{% for x in member.person.email_addresses.all %}
{% if x.publish %}
<li> {{ x.type }}: {{ x.email }} {% if x.primary == "True" %} (Primary) {% endif %} </li>
{% endif %}
{% endfor %}
</ul>
Don't quote True. It's just True:
{% if x.primary == True %}
Or more simply:
{% if x.primary %}

Django regroup not working as expected

I have the following view in my django application
def ViewSale( request ):
salecur = Sale.objects.filter(user=2).order_by('sale_date')
return render_to_response('myapp/sale.html',{'salecur':salecur})
my template looks like this
{% regroup salecur by sale_date as sale_list %}
<ul>
{% for sale_date in sale_list %}
<li>{{ sale_date.grouper }}
<ul>
{% for sale in sale_list %}
<li>{{ sale.item }} - {{ sale.qty }} </li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
When i render the page i get the grouper sale_date.grouper printed, but {{ sale.item }} and {{ sale.qty }} in the inner loop shows nothing! Blank.
What am i missing?
Gath
{% regroup salecur by sale_date as sale_list %}
<ul>
{% for sale_date in sale_list %}
<li>{{ sale_date.grouper }}
<ul>
{% for sale in sale_date.list %}
<li>{{ sale.item }} - {{ sale.qty }} </li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
See the documentation on regroup:
{% regroup %} produces a list of group objects. Each group object has two attributes:
grouper -- the item that was grouped by (e.g., the string "Male" or "Female").
list -- a list of all items in this group (e.g., a list of all people with gender='Male').

How do I check for last loop iteration in Django template?

I have a basic question, in the Django template language how can you tell if you are at the last loop iteration in a for loop?
You would use forloop.last. For example:
<ul>
{% for item in menu_items %}
<li{% if forloop.last %} class='last'{% endif %}>{{ item }}</li>
{% endfor %}
</ul>
{{ forloop.last }}
You can basically use this logic in a for loop:
{% if forloop.last %}
# Do something here
{% endif %}
For example, if you need to put a comma after each item except for the last one, you can use this snippet:
{% for item in item_list %}
{% if forloop.last %}
{{ item }}
{% else %}
{{ item }},
{% endif %}
{% endfor %}
which will become for a list with three items:
first_item, second_item, third_item