On Wed, Mar 9, 2011 at 1:30 PM, octopusgrabbus <old_road_f...@verizon.net>wrote:
> This is for Django 1.2 and MySQL 5.x > > How does a template know a dictionary's name, if a dictionary is > supplied in render_to_response? > > It doesn't. > totals_dict['TotalActiveAccounts'] = ret_list[0] > totals_dict['TotalBillableAccounts'] = ret_list[1] > totals_dict['TotalNon-BillableAccounts'] = int(ret_list[0]) - > int(ret_list[1]) > > context_instance=RequestContext(request)) > return render_to_response('main_page.html', totals_dict, > context_instance=RequestContext(request)) > > Given that code, the template can access TotalActiveAccounts and TotalBillableAccounts: {{ TotalActiveAccounts }} {{ TotalBillableAccounts }} That last key of 'TotalNon-BillableAccounts' is going to be a problem because the dash isn't allowed in a template variable name (though I can't find where allowed chars in template variable name are listed...but trying to access {{ TotalNon-BillableAccounts }} will generate a template syntax error relating to not being able to parse the content after the dash. So you're going to want to avoid putting dashes in your dictionary key names. If for some reason you wanted that dictionary, directly, accessible in the context for the template, you'd pass it inside a different dictionary passed to render_to_response: render_to_response('main_page.html', {'totals_dict': totals_dict }, ... Since I gave the key the same name as the variable with the value, that's the name it will have in the template context, and you can get to the individual items using the . operator, e.g.: {{ totals_dict.TotalActiveAccounts }} If I instead passed the dict under a different name: render_to_response('main_page.html', {'foo': totals_dict }, ... that's the name I'd use in the template: {{ foo.TotalActiveAccounts }} So the variable name for the dictionary in the view makes no difference, it's the key names in the dictionary passed to render_to_response that matter to the template. Karen -- http://tracey.org/kmt/ -- 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.