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

*爱你&永不变心* 提交于 2019-12-09 22:21:11

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!