How do you pass dissimilar data from view to template?

40 views
Skip to first unread message

BillB1951

unread,
Jan 26, 2012, 4:12:07 PM1/26/12
to Django users
I am a relatively new to Django, and have just run into a wall, but I
am sure this will be a cake walk for you veterans out there. I have a
list of items I am displaying in a table in a template. That is no
problem I create an object (list of values) in my view send it to the
template and render the table. However, I would like to also show on
my template a bunch of count()’s displayed as links, that when clicked
will further filter the list of items displayed. For example, I may
have items in the list that cost between $25 and $50, my link would
show that there are say 20 items that match that criteria. When the
link is selected in sends a request to the url.py that in turn
executes a view that further filters the queryset then renders
template again.

How do I get the count() info to the template? I do not think I can
send two separate lists (objects) to the template (at least I have not
been able to figure out how yet). I think I need to get the counts at
the view and then somehow append them to my list object, but I’m not
sure how to do that and also, not quite sure how to parse those values
in the template. I want the counts to show separate from the table
generated from my list object, and I am somewhat concerned I’m going
to mess up my table that is working fine now.

I would appreciate any suggestions you have about how to tackle this,
and I would really appreciate code examples because I am still
somewhat Python/Django code challenged.

Thanks.

Brett Epps

unread,
Jan 26, 2012, 4:55:38 PM1/26/12
to django...@googlegroups.com
It sounds like you're confused about how to add more than one piece of
information to a template. When you render a template, the template is
given a context, which is a Python dict object. A dict is a bunch of
key-value pairs. So, right now, you're probably doing something like:

return render_to_response('path/to/template.html', {'items': items})

Where items is a list of item objects. You can simply add more key-value
pairs to the dict to add your counts:

items_25_to_50_count = Item.objects.filter(price__gte=25,
price__lte=50).count()
return render_to_response('path/to/template.html',
{'items': items, 'items_25_to_50_count': items_25_to_50_count})

Hopefully this helps with your question.

Brett

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

BillB1951

unread,
Jan 26, 2012, 5:01:50 PM1/26/12
to Django users
Brett, Thanks. That is what I needed. BillB1951

On Jan 26, 4:55 pm, Brett Epps <Brett.E...@quest.com> wrote:
> It sounds like you're confused about how to add more than one piece of
> information to a template.  When you render a template, the template is
> given a context, which is a Python dict object.  A dict is a bunch of
> key-value pairs.  So, right now, you're probably doing something like:
>
> return render_to_response('path/to/template.html', {'items': items})
>
> Where items is a list of item objects.  You can simply add more key-value
> pairs to the dict to add your counts:
>
> items_25_to_50_count = Item.objects.filter(price__gte=25,
> price__lte=50).count()
> return render_to_response('path/to/template.html',
>     {'items': items, 'items_25_to_50_count': items_25_to_50_count})
>
> Hopefully this helps with your question.
>
> Brett
>
Message has been deleted

BillB1951

unread,
Jan 27, 2012, 11:31:46 AM1/27/12
to Django users
Thanks for the additional thought on this. It is a much appreciated
confirmation to me. Last night I was torturing my brain over the
logistics of what I had conceived, and by this morning I had pretty
much concluded that I would have to go the route you are suggesting.
Now I just have to find an existing solution ... or learn Javascript.
--Bill

On Jan 27, 11:19 am, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:
> On Thu, 26 Jan 2012 14:01:50 -0800 (PST), BillB1951 <wjbur...@gmail.com>
> wrote:
>
> >Brett, Thanks.  That is what I needed.  BillB1951
>
>         Just an aside: seems like a convoluted plan to keep round-tripping
> to the server/database each time you refine the filter.
>
>         I'm presuming the initial data set being sent is the "full" list. If
> so, might it not be useful to somehow (Caveat: I'm /not/ skilled at
> this, only toyed with Javascript too many years ago) embed a Javascript
> action to count the categories, render the filter selections and, when
> one is picked, have the Javascript modify the page in-place to display
> only the filtered data?
>
>         Granted, adding another language on top of Python, Django, and the
> template language, may be more than desirable.
> --
>         Wulfraed                 Dennis Lee Bieber         AF6VN
>         wlfr...@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Thorsten Sanders

unread,
Jan 27, 2012, 11:44:12 AM1/27/12
to django...@googlegroups.com
If the data is presented as table, datatables is a really nice jquery
plugin for such stuff:

http://datatables.net/

Jonathan Paugh

unread,
Jan 28, 2012, 4:52:17 AM1/28/12
to django...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I think jQuery supports this sort of thing, (as I'm sure other
Javascript libs do). Then again, learning Javascript + jQuery together
seems at least as hard as learning Python + Django.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJPI8VQAAoJEH+72ZVdir1G7P4P/2RjG5MI1NivppUL2gZevkRI
YblHMSwf/RMWX4YjxCbhJNnzAsprNDIwcqKCwfCQo2RCElgz4/r+FTgbA+NN/R81
D6NZAbKqOHWl3jvNKTpytCbMthxRMsE8Frj5gyr1iD4xxEFrYePByOd3Qwml1dCb
3b5aSVQ+4BX84b+vLy8juo/bZb8fQM4oqlu4lUhjqJRGNNSPruXQ9/iom0ugIkyO
QnrAwFnsoJW1+3yGSI7X/p8InWTCznGyFQulOq9cYzmaakXnSjWdLEbg3LCwXqoU
uKJmofVYGVcu5gMXfLhIulJPpCkPgTL+f+Zp91ii49sSgiv2PheqC8aAKv3vhSea
TPF3RzQfkhaUuAA/E/nlagvbOIBfbeSTTy4SG/X5oRS4lD3Lp57BX6g7uiOEeVMV
tgjTRgMq2GrtVxxhL3NSbYhChSWHooNfFN94i8CjZmp3KyjxbVDZuFaeo2PAYlo6
knrkjYWP49mcnM98MetBFe1YupAjJ8lsfMPChfa6Ot/ixGbB+DFk7gnRsnz4D8FU
w7ZBorUiGOclwtwlVpKVOtfT7kb48kzQHf4fidqL3RHZJNusShT94X4fs5VcLOoO
dpDJLwxluBMPpGb7xBMfkhDvdClVDZNnhKYQr0XndydYEDa01ne+4iwHZtsPieaH
R7EFAOahr78c0f8Usj7Y
=JYjK
-----END PGP SIGNATURE-----

Reply all
Reply to author
Forward
0 new messages