Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
989 views
in Technique[技术] by (71.8m points)

format - Django DecimalField Formatting and Filtering Non-Responsive

The DecimalField output on my template is appearing like this; Total Cost: {'Cost__sum': Decimal('739.900000000000')} when it should appear like this Total Cost: 739.90.

Despite my model only having 2 decimal places and trying the DecimalField filters the formatting does not change, it also includes my context variable 'Cost__Sum'. This has not happened in previous apps despite using the same code.

Greatly appreciate if someone could point out what is going wrong here.

Model.py

class ComicInput(models.Model):

        Cost = models.DecimalField(max_digits=10, decimal_places=2, default='1.95')
        Value = models.DecimalField(max_digits=10, decimal_places=2, default='1.95')
        SoldAmt = models.DecimalField(max_digits=10, decimal_places=2, default='0.00')

Views.py

def home(self):
    """Renders the home page.""" 

    #excludelist = ('Sold','Wish')
    if self.user.is_authenticated:
        DisplaySumCost=ComicInput.objects.all().filter( uid=self.user).aggregate(Sum('Cost'))
        DisplaySumValue=ComicInput.objects.all().filter( uid=self.user).aggregate(Sum('Value'))
        DisplaySumSoldAmt=ComicInput.objects.all().filter( uid=self.user).aggregate(Sum('SoldAmt'))
        
  

        assert isinstance(self, HttpRequest)
        return render(
            self,
            'app/index.html',
            {
                'title':'Kollector',
                'year':datetime.now().year,
                'DisplaySumValue': DisplaySumValue,
                'DisplaySumCost': DisplaySumCost,
                'DisplaySumSoldAmt': DisplaySumSoldAmt,
            }
        )
    else:
        assert isinstance(self, HttpRequest)
        return render(
            self,
            'app/index.html',
            {
                'title':'Collector',
                'year':datetime.now().year,
            }
        )

Template

{% extends "app/layout.html" %}
{% load humanize %}
{% load l10n %}
{% block content %}

{% if request.user.is_authenticated %}

    <section style="border:1px solid black;background-color:lightyellow">
        <h3 style="text-align:center"> Collection Summary for {{user.username}} </h3>
            <div class="row">
                <div class="col-md-4">
                   <b>Total Cost: {{DisplaySumCost|localize}} </b>
                   <b>Total Value: {{DisplaySumValue}} </b>
                   <b>Total Sales: {{DisplaySumSoldAmt}} </b>
                </div>

            </div>
    </section>
{% endif %}

{% endblock %}
question from:https://stackoverflow.com/questions/66052175/django-decimalfield-formatting-and-filtering-non-responsive

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...