【问题标题】:How to calculate the total after applying discount in Django?在 Django 中应用折扣后如何计算总数?
【发布时间】:2019-11-05 05:33:37
【问题描述】:

我想计算应用折扣后的总金额。为此,我制作了 cart.py。但是当我从模板.html 中的 cart.py 调用函数时。既不显示打折后的总金额,也不显示打折后的百分比。

cart.py 在购物车应用中创建

from decimal import Decimal
from django.conf import settings
from shop.models import Product
from coupons.models import Coupons

class Cart(object):

def __len__(self):
    return sum(item['quantity'] for item in self.cart.values())

def get_total_price(self):
    return sum(Decimal(item['price']) * item['quantity'] for item in self.cart.values())


def clear(self):
    del self.session[settings.CART_SESSION_ID]
    self.session.modified = True
@property
def coupon(self):
    if self.coupon_id:
        return Coupons.objects.get(id=self.coupon_id)
 def get_discount(self):
    if self.coupon:
        return (self.coupon.discount / Decimal('100')) * self.get_total_price()

 def get_total_price_after_discount(self):
    return self.get_total_price() - self.get_discount()

template.html

<tr class="gray2">
     <td colspan="2"> coupon ({{discount}}) % off</td>
     <td colspan="3"></td>
     <td class="num neg"> {{cart.get_discount|floatformat:"2"}}</td>
</tr>
<tr class="total">
                <td>Total</td>
                <td colspan="4"></td>
                <td class="num">{{cart.get_total_price_after_discount|floatformat:"2"}}</td>
</tr>
</table>
<div class="divo">
    <p>
        coupon code to apply discount
    </p>
    <form action="{% url 'coupons:apply' %}" method="post">
        {{coupon_apply_form}}
        {% csrf_token %}
        <input type="submit" value="apply" class="btn">
</form>
</div>

views.py

@require_POST
def coupon_apply(request):
    now = timezone.now()
    form = CouponApplyForm(request.POST)
    if form.is_valid():
        code = form.cleaned_data['code']
        try:
            coupon = Coupons.objects.get(code__iexact=code,
                                        valid_form__lte=now,
                                        valid_to__gte=now,
                                        active=True)
            request.session['coupon_id'] = coupon.id
        except Coupons.DoesNotExist:
            request.session['coupon_id'] = None

    return HttpResponseRedirect(reverse("cart"))

template.html 的这一部分没有显示。请问有人可以在这方面帮助我。

注意:

  coupon_apply_form = CouponApplyForm()
  context={'cart':cart,'coupon_apply_form':coupon_apply_form}

我已经在购物车应用程序的 view.py 中写了这个。

【问题讨论】:

标签: python django django-models django-templates django-views


【解决方案1】:

您不能从模板中调用函数。在您的视图中进行计算,并将新的总数保存到一个变量中,然后通过上下文将其传递给您的模板。如果您确实需要我描述的实际编码方面的帮助,请告诉我。然而,这是一个重复的问题。

编辑:我建议将来提出描述您遇到的问题的问题,而不是使用仅与您的问题完全相同的细节。作为一个例子,我会问(从 Django 模板调用函数不起作用),尽管它看起来像一个“简单”或“愚蠢”的问题,但它非常简洁。希望这对您将来有所帮助! :)

【讨论】:

  • 感谢您的友好回复。请再告诉我一件事,我可以在 model.py 中进行这些计算,然后在模板中调用它吗?如果是,我应该在views.py 文件中添加上下文吗?我知道我在问愚蠢的问题,但请在这方面指导我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多