Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Basic django coding question - how to get totals on a summary screen [RSS Feed]

#1 Dec. 21, 2010 18:15:26

Casual C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Basic django coding question - how to get totals on a summary screen


I am an experienced developer but new to web development, Python, and
Django. Point me to a good book if you think this is too basic for
this group.

I coded a template that lists a bunch of items. Each item has a
price. In the template, I want to include the sum of all the prices
on a TOTAL line at the bottom.

What's the typical django way to do that? Here's my guess.

Have the template call another function view_total() which gets the
same object_list again and spins through the object_list to tally a
total, and view_total() returns a single object which is a total.

Pointers to sample code that does something like that? Or a book...

Thanks,

Mike

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 21, 2010 18:24:36

Venkatraman S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Basic django coding question - how to get totals on a summary screen


http://www.djangobook.com/Helps in most of the cases.

There are couple of solutions:
1) define a custom tag : pass the list as an argument to this function which
computes the sum
2) when you are passing the list of items from your view to the template,
why not calculate the same in the view itself
and send the sum as parameter to the view
3) Iterate over the list in the template and compute the sum
4) Make an async call from the template to the view which returns the sum.

-V-
<http://about.me/venkasub>http://blizzardzblogs.blogspot.com/--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 21, 2010 19:01:11

Javier G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Basic django coding question - how to get totals on a summary screen


2.1) use the sum() aggregation with the queryset, making the DB do the
work. pass to the template in an extra variable


--
Javier

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Dec. 21, 2010 19:06:13

Casual C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Basic django coding question - how to get totals on a summary screen


Thanks Venkat,

I do like that book -- in fact, it was that book which pointed me to
this google group.

I considered all 4 of those options. Wasn't sure if any of them were
contrary to the philosophy of Django. I wonder if I'm paranoid of an
imaginary code review!

Mike

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Dec. 21, 2010 19:08:06

Casual C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Basic django coding question - how to get totals on a summary screen


Ah. That looks simple. Thanks!

On Dec 21, 2:01 pm, Javier Guerra Giraldez <jav...@guerrag.com> wrote:
> 2.1) use the sum() aggregation with the queryset,

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Basic django coding question - how to get totals on a summary screen [RSS Feed]

Board footer

Moderator control

Enjoy the 21st of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support