重写views.py而不使用locals()
我有以下模板:
<!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>
和相应的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.html', locals())
如何重写上面的函数,使其不使用 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.html', locals())
How do I re-write the function above such that it doesn't use locals()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的视图可以是:
由于您必须迭代元来生成模板,因此您不必在视图中执行此操作。 render_to_response 的第二个参数也可以是要添加到模板上下文中的键字典。
Your view can just be:
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.