Rewrite views.py without using locals()
I have the following template:
<!DOCTYPE HTML PUBLIC "=//W3C//DTD HTML 4.01//EN">
<html land="en">
<head>
<title>Some Meta Data</title>
</head>
<body>
<ul>
{% for key,values in meta %}
<li> {{ key }}, {{ values }} </li>
{% endfor %}
</ul>
</body>
</html>
And corresponding views.py:
def display_meta(request):
meta = request.META.items()
metadata = []
for k,v in meta:
key = k
values = v
return render_to_response('meta.h开发者_JAVA百科tml', locals())
How do I re-write the function above such that it doesn't use locals()
?
Your view can just be:
def display_meta(request):
meta = request.META.items()
return render_to_response('meta.html', {"meta": meta})
Since you'll have to iterate meta to generate the template you don't have to do it in the view. Also the second argument of render_to_response can be a dictionary of keys to add into the template context.
精彩评论