title: Accessing dict values in Django template
body: <p>I'm having trouble accessing the values of a dict object that I'm trying to send to a template in Django. I can access and print the dict's contents in my view, but when I try to send the data to my template there are all kinds of characters like it isn't encoded properly. At first I thought it might have been an issue with serializing, but I found <a href="https://stackoverflow.com/questions/14794677/str-object-has-no-attribute-meta">this post</a> which states that json_serializer.serialize is supposed to be used with a queryset. Is this correct? From here I tried the following.</p>
  
  <p>At first I tried to just send the data variable by itself without any formatting like so</p>
  
  <pre><code>data = {'item_1': 123, 'item_2': 456, 'item_3': ['a','b','c'] }
  return render(request, 'testsite/new_page.html', {'data' : data} )
  </code></pre>
  
  <p>In the template I have the following</p>
  
  <pre><code>&lt;script&gt;var data = "{{ data }}"; &lt;/script&gt;
  &lt;script&gt;console.log(data);&lt;/script&gt;
  
  // displays the following
  // &amp;lt;testsite.views.data object at 0x1045f1e48&amp;gt;
  </code></pre>
  
  <p>Then I tried to format the data to JSON</p>
  
  <pre><code>data = {'item_1': 123, 'item_2': 456, 'item_3': ['a','b','c'] }
  return render(request, 'testsite/new_page.html', {'data' : json.dumps(data.__dict__) } )
  
  //template
  &lt;script&gt;var data = "{{ data }}"; &lt;/script&gt;
  &lt;script&gt;console.log(data);&lt;/script&gt;
  
  // the following is the improperly formatted result
  // {&amp;quot;item_1&amp;quot;: 123, &amp;quot;item_2&amp;quot;: 456, &amp;quot;item_3&amp;quot;: [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;,]
  </code></pre>
  
  <p>If I don't use <code>json.dumps(data.__dict__)</code> and instead just have <code>json.dumps(data)</code> I get an error about the object is not JSON serializable </p>
  
  <p>In my view I have the following. The contents of the dict are properly formatted.</p>
  
  <pre><code>print(data.__dict__)
  import pdb; pdb.set_trace()
  
  # displays the dict properly
  # {'item_1': 123, 'item_2': 456, 'item_3': ['a','b','c'] }
  </code></pre>
  
  <p>In the template tried to iterate through the data object using javascript like so, but it just displays each individual character of the improperly formatted dict.</p>
  
  <pre><code>for (var key in data) {
      console.log(data[key]);
  }
  </code></pre>
  
  <p>What am I doing wrong that is causing the improperly formatted data in the template? </p>