Django通过正则跳转查看详细信息页面

Django通过正则跳转查看详细信息页面

Django通过正则跳转查看详细信息页面

python views视图

USER_DICT = {
    '1':{'name':'root1','email':'root@123'},
    '2':{'name':'root2','email':'root@123'},
    '3':{'name':'root3','email':'root@123'},
    '4':{'name':'root4','email':'root@123'},
}

def dict(request): 
    return render(request, 'dict.html',{'user_dict':USER_DICT})


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

Django模板代码

dict.html


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

</body>
</html>

detail.html

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

</body>
</html>

url


urlpatterns = [
    path('admin/', admin.site.urls),
    path('login',views.login),
    path('signup.html',views.signup),
    path('home',views.home.as_view()),
    path('dict',views.dict),             #dict
    re_path('detail',views.detail), #detail
]


结果展示

Honeycam 2019-12-09 22-01-22

posted on 2019-12-09 22:05  柳条儿  阅读(206)  评论(0编辑  收藏  举报

导航