Re: sorting model data in template page

2011-04-29 Thread Victor Harada
The for tag does not accept calls with parameter.

You should make the view function calculate the sorted list and pass it to
the template or create a template tag that accepts your call

2011/4/29 Krish 

> This code works perfectly,
>
> {% for talk_child in talk.child.all %}
> {{talk_child.text}} {{talk_child.created_dt}}
> {% endfor %}
>
> Now, I want to sort all child based on created_dt ASC (something like
> below), but it doesn't work. How can I get this right?
>
> {% for talk_child in talk.child.order_by 'created_dt' %}
> {{talk_child.text}} {{talk_child.created_dt}}
> {% endfor %}
>
> Below is my class
> class Talk(models.Model):
>user = models.ForeignKey(User)
>destination = models.ForeignKey(Destination)
>text = models.TextField()
>type = models.CharField(max_length=30)
>sup = models.ForeignKey('self', blank=True, null=True,
> related_name='child')
>created_dt = models.DateTimeField(auto_now_add=True)
>thumb_up = models.IntegerField()
>thumb_down = models.IntegerField()
>
> class Meta:
>ordering = ["-created_dt"]
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

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



Re: Sum in html template using template tag

2011-03-11 Thread Victor Harada
Hi

You should use {{ list | running_total }}

and the filter should look like this:

from django.template import Library
register = Library()
@register.filter
def running_total(role_total):
 return sum( [d.get('total') for d in role_total] )

2011/3/11 sushanth Reddy 

> Hi ,
>
> I am trying to sum in HTML,but template tag return 0 ,
>
>
> View.py
>
> def gen_Report(request):
>
> ### query returns below output
> list=[{'total': 1744, 'user': u'x'}, {'total': 13, 'user': u'y'},
> {'total': 126, 'user': u'z'}, {'total': 46, 'user': u'm'}, {'total': 4,
> 'user': u'n'}, {'total': 8, 'user': u'o'},  {'total': 3, 'user': u'p'}]
>
> return render_to_response('user.html', locals(),
> context_instance = RequestContext(request))
>
> Template :
>
> user.html
>
>   {% load temptags %}
>
>  
> 
> 
>
> S.No
> role
> Count
>
> 
> 
> {% for fetch in list %}
>
> 
> {{forloop.counter}}
> {{fetch.user}}
> {{fetch.total}}
>
>
>
> {% endfor %}
> {{ list.total|running_total}}
> 
>
> 
>
> Template tag:
>
>
> from django.template import Library
> register = Library()
> @register.filter
> def running_total(role_total):
>   return sum(d.get('list_sum') for d in list_total)
>
>
>
> output :
>
> S.Nouser  Count
> 1  x  1744
> 2  y13
> 3  z126
> 4  m46
> 5  n  4
> 6  o 8
> Sum-->   0  (it returns zero)
>
> I am doing anything wrong here ?
>
> can you please help me,how to return sum total using template tag here ?
>
>
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

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