Django2中基于正则表达式的URL
学习Django时遇到正则表达式浏览器一直显示页面不存在解决办法
url.py
from django.contrib import admin
from app1 import views
from django.urls import re_path,path
from django.conf.urls import url
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index),
url('^detail-(\d+).html', views.detail),
]
views.py
from django.shortcuts import render
# Create your views here.
DI={
'1':{'name':'root1' ,'email':'123@root'},
'2': {'name': 'root2', 'email': '123@root'},
'3': {'name': 'root3', 'email': '123@root'},
'4': {'name': 'root4', 'email': '123@root'}
}
def index(request):
return render(request,'index.html',{'dict':DI})
def detail(request,nid):
detail_info = DI[nid]
return render(request, "detail.html", {'detail_info': detail_info})
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{dict.k1}}
<ul>
{% for k,foo in dict.items %}
<li><a target="_blank" href="/detail-{{ k }}.html">{{ foo.name }}</a></li>
{% endfor %}
</ul>
</body>
</html>
detail.py
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>详细信息</h3>
<h5>用户名:{{ detail_info.name }}</h5>
<h5>邮箱:{{ detail_info.email }}</h5>
</body>
</html>

浙公网安备 33010602011771号