Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7971665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:41:18+00:00 2026-06-04T07:41:18+00:00

Possible Duplicate: Django template can’t loop defaultdict I am wondering why my defaultdict(list) will

  • 0

Possible Duplicate:
Django template can’t loop defaultdict

I am wondering why my defaultdict(list) will show when I test it in my views.py but when I go to show the data on my template, I get nothing, not even an error.

Any suggestions?

Here is my views.py – confirm_list is my defaultdict(list)

def confirmations_report(request, *args, **kwargs):
from investments.models import Investment, InvestmentManager
from reports.forms import ConfirmationsForm
from collections import defaultdict
import ho.pisa as pisa
import cStringIO as StringIO
import os.path
confirm_list = defaultdict(list)
context = {}

if request.POST:
    form = ConfirmationsForm(request.POST)
    if form.is_valid():
        start_date = form.cleaned_data['start_date']
        end_date = form.cleaned_data['end_date']
        investments = Investment.objects.all().filter(contract_no = "",maturity_date__range=(start_date, end_date)).order_by('financial_institution')
        for i in investments:
            confirm_list[i.financial_institution.pk].append({
                'fi':i.financial_institution,
                'fi_address1': i.financial_institution.address1,
                'fi_address2': i.financial_institution.address2,
                'fi_city': i.financial_institution.city,
                'fi_prov': i.financial_institution.state_prov,
                'fi_country': i.financial_institution.country,
                'fi_postal': i.financial_institution.postal,
                'primary_owner': i.plan.get_primary_owner().member,
                'sin': i.plan.get_primary_owner().member.client.sin,
                'type': i.product.code,
                'purchase_amount': i.amount,
                'purchase_date': i.start_date,
            })
            context['investments'] = investments
        context['confirmlist'] = confirm_list
        for key, value in confirm_list.items():
            print key, value
        context['inv'] = investments
    if request.POST.has_key('print_report_submit'):
        context['show_report'] = True
        context['mb_logo'] = os.path.join(os.path.dirname(__file__), "../../../media/images/mb_logo.jpg")
        html = render_to_string('reports/admin/confirm_report_print.html', RequestContext(request,context))
        result = StringIO.StringIO()
        pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
        response = HttpResponse(result.getvalue(), mimetype='application/pdf')
        response['Content-Disposition'] = 'attachment; filename=unreceived-confirmations.pdf'
        return response

else:
    form = ConfirmationsForm()

context['form'] = form
return render_to_response('reports/admin/confirm_report.html', RequestContext(request, context))

But when I do:

for key, value in confirm_list.items():
            print key, value

On my template like so:

{% extends 'reports/admin/base.html' %}
{% load humanize %}

{% block report_html %}
<h3>Unreceived Confirmations Report</h3>
<form method="post" action="">
<table>
    <tr>
        <td>
            <strong> {{ form.start_date.label }}</strong> {{ form.start_date }}
            <strong>{{ form.end_date.label }}</strong> {{ form.end_date }}
        </td>
    </tr>
</table>
<input type="submit" value="View Report">
    <input type="submit" name="print_report_submit" value="Print Report"/>
</form>
    {% for key, value in confirmlist.items %}
        {{ key }} - {{ value }}
    {% endfor %}
{% endblock %}

I get nothing.

Here is a sample of the output I get when testing in the views.py

33 [{'fi_address1': u'Scotiabank FAS', 'fi_country': u'Canada', 'fi_address2': u'20 Queen Street West, Suite 2600', 'fi_city': u'TORONTO', 'fi': <FinancialInstitution: NATIONAL TRUST>, 'fi_prov': u'Ontario', 'fi_postal': u'### ###', 'purchase_amount': Decimal('30000.00'), 'purchase_date': datetime.date(2011, 6, 27), 'type': u'GIC', 'sin': u'###/###/###', 'primary_owner': <Member: #, #>}]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:41:18+00:00Added an answer on June 4, 2026 at 7:41 am

    This happens because of the way the Django template language does variable lookups. When you try to loop through the dictionaries items,

    {% for key, value in confirmlist.items %}
    

    Django first does a dictionary lookup for confirmlist['items']. As this is a defaultdict, an empty list is returned.

    It’s a cruel gotcha, that I’ve been stung by as well!

    To work around this problem, convert your defaultdict into a dictionary before adding it to the template context.

    context['confirmlist'] = dict(confirm_list)
    

    Or, as explained by sebastien trottier in his his answer to a similar question, set
    default_factory to None before adding to the template context.

    confirm_list.default_factory = None
    context['confirmlist'] = confirm_list
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Django data migration when changing a field to ManyToMany how I can
Possible Duplicate: How can I pass data to any template from any view in
Possible Duplicate: django - ordering queryset by a calculated field How can i use
Possible Duplicate: Override django-admin edit form field values for encrypted data The inline model
Possible Duplicate: Django - Iterate over model instance field names and values in template
Possible Duplicate: Putting a django login form on every page how can I add
Possible Duplicate: How to put braces in django templates? When I use jQuery template
Possible Duplicate: How can I include custom modules in a Django app There are
Possible Duplicate: Django switching, for a block of code, switch the language so translations
Possible Duplicate: How to: URL re-writing in PHP? How can a website use an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.