I'm learning django from djangobook.com
As an exercise, I'm trying to print all HttpRequest.META
dictionary in the form of a table using template.
views.py contains
# Create your views here.
from django.http import HttpResponse
from django.shortcuts import render
def http_headers(request):
return render(request,'headers.html',{'headers':request.META})
headers.html <-- template
<html><body><table border="1">
{% for k in headers.keys %}
<tr><td> {{ k }} </td><td>{{ headers.k }}</td></tr>
{% endfor %}
</table></body></html>
Output:
<html><body><table border="1">
<tr><td> TMP </td><td></td></tr>
<tr><td> COMPUTERNAME </td><td></td></tr>
<tr><td> wsgi.multiprocess </td><td></td></tr>
<tr><td> RUN_MAIN </td><td></td></tr>
<tr><td> HTTP_COOKIE </td><td></td></tr>
...
...
Problem:
Why is it unable to access {{headers.k}}
?
djangobook.com says:
Dot lookups can be summarized like this: when the template system encounters a dot in a variable name, it tries the following lookups, in this order:
Dictionary lookup (e.g., foo["bar"]) Attribute lookup (e.g., foo.bar) Method call (e.g., foo.bar()) List-index lookup (e.g., foo[2])
So, headers.k
must first match the dictionary lookup since headers is a dictionary. Right?
What am I missing