Calculate Total Sales in View

from django.shortcuts import render
from .models import Sale

def sales_report(request):
    sales = Sale.objects.all()
    total_sales = sales.aggregate(total=models.Sum('total_price'))['total']
    return render(request, 'sales_report.html', {'sales': sales, 
'total_sales': total_sales})


*Calculate Total Sales in View*:

  from flask import Flask, render_template
from models import db, Sale

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///sales.db'
db.init_app(app)

@app.route('/sales_report')
def sales_report():
    sales = Sale.query.all()
    total_sales = sum(sale.total_price for sale in sales)
    return render_template('sales_report.html', sales=sales, 
total_sales=total_sales)

if __name__ == '__main__':
    app.run(debug=True)


Sales
------------------------------------------------------------
| Product   | Date       | Amount | Quantity Sold | Total Price |
------------------------------------------------------------
| Product A | 2024-06-01 | 100.00 | 2             | 200.00      |
| Product B | 2024-06-02 | 150.00 | 1             | 150.00      |
| Product A | 2024-06-03 | 100.00 | 3             | 300.00      |
------------------------------------------------------------
Total Sales: 650.00


On Thursday 6 June 2024 at 08:24:46 UTC+5:30 Nagaraja wrote:

> try below method
>
> <body>
>     {% block content %}
>     {% endblock content %}
> </body>
>
>
> On Wed, Jun 5, 2024 at 1:09 PM 'Mee “MeeGrp” Grp' via Django users <
> [email protected]> wrote:
>
>> I have two html files 1. view_sales.html  2. total_sales.html
>>
>> I want to display output of total_sales.html within  view_sales.html* 
>> just below *
>> *view_sales.html output*
>>
>> Your help would be highly appreciated !!!!!!!!
>>
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected].
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/30b6b324-22b2-44f1-b2dd-b0b034ac2f61n%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/django-users/30b6b324-22b2-44f1-b2dd-b0b034ac2f61n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/96e52f98-0225-4f2c-b210-41458a913dd1n%40googlegroups.com.

Reply via email to