前端循环字典的方式

index.html

 {{ user_dict.k1 }}
    <ul>
        {% for k,row in user_dict.items %}
            <li><a  target="_blank"  href="/CC/detail/?nid={{ k}}">{{ row.name }}</a></li>
        {% endfor %}
    </ul>
    <p>_______________________________________________________</p>
    <ul>
        {% for k,row in user_dict.items %}
            <li><a  target="_blank"  href="/CC/detail-{{ k}}.html">{{ row.name }}</a></li>
        {% endfor %}
    </ul>

    <ul>
        {% for k in user_dict.keys %}
            <li>{{ k }}-</li>
        {% endfor %}
    </ul>
    <ul>
        {% for row in user_dict.values %}
            <li>-{{ row }}</li>
        {% endfor %}
    </ul>

  后台代码:

USER_DICT={
    "1":{"name":"root1","email":"123"},
    "2":{"name":"root2","email":"123"},
    "3":{"name":"root3","email":"123"},
    "4":{"name":"root4","email":"123"},
}
def index(request):
    return  render(request,"index1.html",{'user_dict':USER_DICT})

  

def detail(request):
    print '2222222222222',request.GET
    nid =request.GET.get("nid")
    detail_info = USER_DICT[nid]
    return render(request,'detail.html',{"dict":detail_info})

  detail.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
        <h3>详细信息</h3>
        <p> name :{{ dict.name }}</p>
        <p> email :{{ dict.email }}</p>


</body>
</html>

  

  

 

posted @ 2018-04-29 17:02  梦中琴歌  阅读(1542)  评论(0编辑  收藏  举报