Reputation: 1633
I am trying to complete the simple exercise near the top of the page here: http://www.djangobook.com/en/2.0/chapter07/. When the user goes to mysite.com/meta/ I want to display a simple html table on the page showing the HTTPRequest meta data.
In my urls.py
file I have:
from django.conf.urls.defaults import patterns, include, url
from mysite.views import display_request_meta_data
urlpatterns = patterns('',
url(r'^meta/$', display_request_meta_data),
)
In my views.py
file I have:
from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
import logging
logger = logging.getLogger(__name__)
def display_request_meta_data(request):
meta_data = request.META.items()
meta_data.sort()
for x in meta_data
# print x # wasn't working, so I tried using a logger instead.
logger.error(x)
t = get_template('http_meta_data_table.html')
html = t.render(Context(*meta_data_dict))
return HttpResponse(html)
In my templates directory I have a file called http_meta_data_table.html
which contains the following:
<html>
<head>
<title>HTTP Meta Data</title>
</head>
<body>
<table>
{% for key, value in meta_data_dict %}
<tr><td>{{key}}</td><td>{{value}}</td></tr>
{% endfor %}
</table>
</body>
</html>
Upvotes: 0
Views: 99
Reputation: 597341
What about:
def display_request_meta_data(request):
meta_data = request.META.items()
meta_data.sort()
for x in meta_data: # you were missing a semi column
# print x # wasn't working, so I tried using a logger instead.
logger.error(x)
t = get_template('http_meta_data_table.html')
html = t.render(Context({'meta_data': meta_data})) # the dict doesn't exitst
return HttpResponse(html)
In html, the dict doesn't exist either:
<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>
This has really nothing to do with django, these are very basic error. Learning programming while using a framework is a bad idea IMO. You should learn the basics before, or your learnin curve is going to look like a straigth wall.
Django is an easy framework to learn, but it assumes you know Python.
Upvotes: 1
Reputation: 7287
meta_data_dict
is undefined. You probably was to pass meta_data
to t.render
Upvotes: 0