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

2

6

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.

Episodic answered 24/2, 2020 at 18:31 Comment(0)
E
3

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 %}
Equanimity answered 25/2, 2020 at 4:41 Comment(4)
Thanks but I'm still seeing some odd results. Does "widthratio" not deal with floats? This {% widthratio .3 1 .9 as answer %} produces "answer" = 0, which is not correct and not what I want.Episodic
It does deal with floats, but it rounds the result to the nearest integer. It's intended primarily for calculating image width ratios and might not be suitable for what you want. I think you would actually be better off just writing your own template tag to handle your logic.Equanimity
Hmm, ok before I'm still somewhat of a novice in Django, but I don't think I need to write my own custom tag to do multiplication. Maybe I'm wrong. I'll Google around.Episodic
Django's philosophy regarding what you should/shouldn't do in templates is documented here. "If you have a background in programming, or if you’re used to languages which mix programming code directly into HTML, you’ll want to bear in mind that the Django template system is not simply Python embedded into HTML. This is by design: the template system is meant to express presentation, not program logic." (there's more below that in the link).Equanimity
A
0

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.

Allowable answered 4/3, 2020 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.