Django的正則表達式的URL
創建項目

創建App
python manage.py startapp zhengce

設置settings.py
1.INSTALLED_APPS中加入App(zhengce)
2.注釋csrf
3.靜態文件路徑
STATICFILES_DIRS=(
os.path.join(BASE_DIR,'static'),
)



urls.py文件 from zhengce import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/', views.index), url(r'^detail-(?P<nid>\d+)-(?P<uid>\d+)', views.detail),
views.py文件 from django.shortcuts import render,HttpResponse,redirect # Create your views here. USER_INFOR={ '1':{'name':'root1','email':'root1@163.com'}, '2':{'name':'root2','email':'root2@163.com'}, '3':{'name':'root3','email':'root3@163.com'}, '4':{'name':'root4','email':'root4@163.com'}, } def index(request): return render(request, 'index.html', {'USER_INFOR': USER_INFOR}) def detail(request,nid, uid): print(nid,uid) # return HttpResponse(nid) detail_info = USER_INFOR[nid] return render(request,'detail.html',{'detail_info':detail_info})
templates目錄下的detail.html和index.html
index.html頁面 <body> <ul> {% for k,v in USER_INFOR %} <li><a target="_blank" href="/detail-{{ k }}.html">-{{ v.name }}</a></li> {% endfor %} </ul> </body>
detail.html頁面 <body> <h1>詳細信息</h1> <h6>用戶名:{{ detail_info.name }}</h6> <h6>郵箱:{{ detail_info.email }}</h6> </body>
預期效果

