django实例之点击查询详细内容1

2017-12-13  本文已影响0人  后来者2016

开发需求:
index页面是一个标题的列表,点击其中一条信息,可以跳转到detail页面查看其详细内容。暂不使用数据库,数据从USER_DICT 字典中获取

views.py

USER_DICT = {
    '1' : {'name': 'root1', 'email':'root@123.com'},
    '2' : {'name': 'root2', 'email':'root@123.com'},
    '3' : {'name': 'root3', 'email':'root@123.com'},
    '4' : {'name': 'root4', 'email':'root@123.com'},
    '5' : {'name': 'root5', 'email':'root@123.com'},
}
def index(req):
    return render(req,'index.html',{'user_dict':USER_DICT})

def detail(req):
    nid = req.GET.get('nid')
    detail_info = USER_DICT[nid]
    return render(req,'detail.html',{'detail_info':detail_info})

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
    <ul>
        {% for k,v in user_dict.items %}
            <li><a href="/detail/?nid={{ k }}">{{ v.name }}</a></li>
        {% endfor %}
    </ul>

</body>
</html>

details.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>detail</title>
</head>
<body>
    <h1>详细信息</h1>
    <h6>用户名 {{ detail_info.name }}</h6>
    <h6>邮箱 {{ detail_info.email }}</h6>
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读