count

65 views
Skip to first unread message

sum abiut

unread,
Jan 25, 2016, 9:07:43 PM1/25/16
to django...@googlegroups.com
Hi,
i am trying to count the number of fields that has request field begin checked and then display the value of count in a template.html but i am not sure how to go about doing that.

For example i want to do something like

test=ball.object.filter(request="checked").count()

then on a template.html i want to out put the value of test

{{test}}

i have try that but i am not getting any value from the output.


Thanks in advance

Cheers

James Schneider

unread,
Jan 25, 2016, 9:31:13 PM1/25/16
to django...@googlegroups.com

Can you post your view? You would run that query in your view and then pass it to your template through the context.

-James

sum abiut

unread,
Jan 25, 2016, 9:45:52 PM1/25/16
to django...@googlegroups.com
Thanks James,
here is my view.py

def foreginexchange_view(request):
    test=ball.objects.filter(request="checked").count()
    return render(request,'template.html',locals())



Cheers




-James

--
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 django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUgThuMtZvUj3QHxLKFHiRu67oTZuvfoLT3K9EpNt08Dg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.




aspe...@gmail.com

unread,
Jan 26, 2016, 6:05:32 AM1/26/16
to Django users
Try this
def foreginexchange_view(request):   
    t = loader.get_template("template.html")
    test=ball.objects.filter(request="checked").count()   
    return HttpResponse(t.render({'test': test}))

Andreas Schosser

unread,
Jan 26, 2016, 9:13:49 AM1/26/16
to django...@googlegroups.com
Hi

> i am trying to count the number of fields that has request field begin
> checked and then display the value of count in a template.html but i am not
> sure how to go about doing that.

If you pass your object to the template you can use the "length" filter:

{{ test|length }}

Andreas

--
Kurs 10 IT-Consulting www.kurs-10.de
Andreas Schosser a...@kurs-10.de

Baldestraße. 14 Telefon +49 89 41615842-0
80469 München Telefax +49 89 41615842-3

0x6EDECCF1 - 2AA0 939B 5585 819B FCE8 E43B 0B8E 0DF2 6EDE CCF1

James Schneider

unread,
Jan 26, 2016, 1:55:31 PM1/26/16
to django...@googlegroups.com
On Tue, Jan 26, 2016 at 6:12 AM, Andreas Schosser <a...@kurs-10.de> wrote:
Hi

> i am trying to count the number of fields that has request field begin
> checked and then display the value of count in a template.html but i am not
> sure how to go about doing that.

If you pass your object to the template you can use the "length" filter:

{{ test|length }}

I would only do this if {{ test }} contains objects that you actually want to process/display elsewhere in the template. If you only need the count, I would recommend sticking with the .count() query and passing it through the context since that is faster and more efficient to process, especially for items like votes or likes, where the numbers may reach into the thousands or more.

-James

sum abiut

unread,
Jan 26, 2016, 4:14:03 PM1/26/16
to django...@googlegroups.com
When i did that i got the error:

'dict' object has no attribute 'render_context'



For more options, visit https://groups.google.com/d/optout.



-

aspe...@gmail.com

unread,
Jan 27, 2016, 6:42:37 AM1/27/16
to Django users
Which version of Django do you using?

вторник, 26 января 2016 г., 23:14:03 UTC+2 пользователь suabiut написал:

sum abiut

unread,
Jan 27, 2016, 4:12:19 PM1/27/16
to django...@googlegroups.com

aspe...@gmail.com

unread,
Jan 29, 2016, 12:09:58 AM1/29/16
to Django users
from django.http import HttpResponse
from django.template import loader, RequestContext


def foreginexchange_view(request):
    t = loader.get_template("template.html")
    test=ball.objects.filter(request="checked").count()
    c = RequestContext(request, {'test': test})
    return HttpResponse(t.render(c))

sum abiut

unread,
Jan 29, 2016, 1:31:20 AM1/29/16
to django...@googlegroups.com
Thanks heaps works perfectly well.

Cheers

Reply all
Reply to author
Forward
0 new messages