Python 在 Django 模板中相乘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19588160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiply in django template
提问by vijay shanker
I am looping through cart-items, and want to multiply quantity with unit-price like this:
我正在遍历购物车项目,并希望将数量与单价相乘,如下所示:
{% for cart_item in cart.cartitem_set.all %}
{{cart_item.quantity}}*{{cart_item.unit_price}}
{% endfor %}
Is it possible to do something like that? any other way to do it !! Thanks
有可能做这样的事情吗?任何其他方式来做到这一点!谢谢
采纳答案by Brandon
You need to use a custom template tag. Template filters only accept a single argument, while a custom template tag can accept as many parameters as you need, do your multiplication and return the value to the context.
您需要使用自定义模板标签。模板过滤器只接受一个参数,而自定义模板标签可以根据需要接受任意数量的参数,进行乘法并将值返回给上下文。
You'll want to check out the Django template tag documentation, but a quick example is:
您需要查看 Django模板标签文档,但一个简单的例子是:
from django import template
register = template.Library()
@register.simple_tag()
def multiply(qty, unit_price, *args, **kwargs):
# you would need to do any localization of the result here
return qty * unit_price
Which you can call like this:
你可以这样调用:
{% load your_custom_template_tags %}
{% for cart_item in cart.cartitem_set.all %}
{% multiply cart_item.quantity cart_item.unit_price %}
{% endfor %}
Are you sure you don't want to make this result a property of the cart item? It would seem like you'd need this information as part of your cart when you do your checkout.
您确定不想将此结果作为购物车项目的属性吗?当您结账时,您似乎需要将此信息作为购物车的一部分。
回答by iblazevic
You can do it in template with filters.
您可以使用过滤器在模板中执行此操作。
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters
From documentation:
从文档:
Here's an example filter definition:
这是一个示例过滤器定义:
def cut(value, arg):
"""Removes all values of arg from the given string"""
return value.replace(arg, '')
And here's an example of how that filter would be used:
这是如何使用该过滤器的示例:
{{ somevariable|cut:"0" }}
回答by Martin
Or you can set the property on the model:
或者您可以在模型上设置属性:
class CartItem(models.Model):
cart = models.ForeignKey(Cart)
item = models.ForeignKey(Supplier)
quantity = models.IntegerField(default=0)
@property
def total_cost(self):
return self.quantity * self.item.retail_price
def __unicode__(self):
return self.item.product_name
回答by Rahman
You can use widthratio
builtin filter for multiplication and division.
您可以使用widthratio
内置过滤器进行乘法和除法。
To compute A*B:{% widthratio A 1 B %}
计算 A*B:{% widthratio A 1 B %}
To compute A/B:{% widthratio A B 1 %}
计算 A/B:{% widthratio A B 1 %}
source: link
来源:链接
Notice: For irrational numbers, the result will round to integer.
注意:对于无理数,结果将四舍五入为整数。