Thursday, January 26, 2012

Re: How do you pass dissimilar data from view to template?

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


On 1/26/12 3:12 PM, "BillB1951" <wjburris@gmail.com> wrote:

>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.
>
>--
>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+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment