Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
Django template not displaying looped keys, values

I'm very new to Django and am trying to figure out why I can call the keys in a dict in my template as expected, but looping through the dict does not produce any text, nor any error messages. I don't want to hard code the key names (i.e. below msgid in my template, because the dict is dynamic and so I only want to loop through it.

views.py

class Pymat_program(View):
    def get(self, request, *args, **kwargs):
        selected_xml = 'a_directory_to_my_file'
        smsreport_dict = self.parse_xml_file(selected_xml)
        html = self.populate_html_text(smsreport_dict)

        return HttpResponse(html)

    def populate_html_text(self, smsreport_dict):
        t = get_template('template2.html')
        html = t.render(smsreport_dict)

        return html

template2.html

    <p>MSGID: {{ msgid }}</p>

    <p> Begin
    {% for key, value in smsreport_dict %}
        <tr>
            <td> Key: {{ key }} </td> 
        </tr>
    {% endfor %}
    </p>End

In the template2.html you can see the msgid value (one of several values in smsreport_dict) being called, which is displayed on my page. But for some reason the smsreport_dict looping produces no text. Where am I going wrong?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

smsreport_dict should be inside the Context you use in order to render the template:

...
html = t.render(Context({"smsreport_dict": smsreport_dict}))
...

Plus, you forgot to call .items when iterating through the dict in the template:

{% for key, value in smsreport_dict.items %}
  ...
over 4 years ago · Santiago Trujillo Report

0

You need to add .items to smsreport_dict

MSGID: {{ msgid }}

<p> Begin
{% for key, value in smsreport_dict.items %}
    <tr>
        <td> Key: {{ key }} </td> 
    </tr>
{% endfor %}
</p>End

See here for a similar situation.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!