Django(part15)--页面跳转

学习笔记,仅供参考,有错必纠


模板


页面跳转


因为我不知道咋描述,所以直接放代码,我们一起来细细品味。


pages.html

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Yes!</title>
</head>
<body>
	<ul>
		<li><a href="/page2_template/" >第2个模板</a></li>
		<li><a href="/page3_template/" >第3个模板</a></li>
		<li><a href="/page4_template/" >第4个模板</a></li>
	</ul>
	
</body>
</html>

我们在pages.html页面中添加了3个通往其他页面的超链接.


urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r'page2_template/$', views.page2_template),
    re_path(r'page3_template/$', views.page3_template),
    re_path(r'page4_template/$', views.page4_template),
    re_path(r'pages/$', views.pages),
]

views.py

def pages(request):
    return render(request, "pages.html")

向http://127.0.0.1:8000/pages/发起请求:

点击"第2个模板":

可以看到,我们成功跳转到路由为page2_template/的页面

猜你喜欢

转载自blog.csdn.net/m0_37422217/article/details/106820702
今日推荐