开发者

Rewrite views.py without using locals()

开发者 https://www.devze.com 2023-02-13 21:18 出处:网络
I have the following template: <!DOCTYPE HTML PUBLIC \"=//W3C//DTD HTML 4.01//EN\"> <html land=\"en\">

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消