Here is my view. I am trying to simply display a table with the http meta data.
def display_request_meta_data(request):
meta_data = request.META.items()
meta_data.sort()
for x, y in meta_data: # output of this loop pasted below
print x, y
t = get_template('http_meta_data_table.html')
html = t.render(Context(meta_data)) # I don't understand how to write this line correctly.
return HttpResponse(html)
Here is what http_meta_data_table.html looks like:
<html>
<head>
<title>HTTP Meta Data</title>
</head>
<body>
<table>
{% for key, value in meta_data %}
<tr><td>{{key}}</td><td>{{value}}</td></tr>
{% endfor %}
</table>
</body>
</html>
The loop that prints x, y returns the following (truncated, because there is a bunch of it):
Apple_PubSub_Socket_Render /tmp/launch-SvFjVB/Render
COLORFGBG 7;0
COMMAND_MODE unix2003
CONTENT_LENGTH
CONTENT_TYPE text/plain
CSRF_COOKIE de982574e125805e307091fcd3f25d2e
DISPLAY /tmp/launch-d6usP9/org.x:0
DJANGO_SETTINGS_MODULE mysite.settings
EDITOR mate -w
GATEWAY_INTERFACE CGI/1.1
HOME /Users/me
etc...
Contextexpects a dictionary, mapping keys to values. By callingitems(), you got a list of tuples instead:Even if you turned this back into a dict (which will lose the ordering), you still cannot use it as a context, because the template doesn’t know a priori what keys are contained in it. Instead, pass
meta_dataas a value inside the dict:Now saying
meta_datain your template will call up the value associated with the keymeta_data, which is a list, so yourfortag will iterate over that list.