day18-Django基于正则表达式的URL

一、前言

  我们今天来说说Django基于正则表达式的URL,我们经常通过参数不同,写一个同一个页面,只是最后面的nid不一样,那这个如何做呐?接下来我们就来试试。

二、根据id获取页面

2.1、通过接口的方式

说明:http://127.0.0.1:8000/detail/?nid=3  用这种方式 来访问

1、project.url.py配置

from django.contrib import admin
from django.urls import path
from app01 import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/',views.index),
    path('detail/',views.detail),
]

2、模板中index.html的代码

<body>
    <ul>
        {% for k,row in user_dict.items %}
            <li><a target="_blank" href="/detail/?nid={{ k }}">{{ row.name }}</a></li>
        {% endfor %}
    </ul>
</body>

3、app01.views.py

USER_DICT = {
    "1":{"name":'root1',"email":"root1@126.com"},
    "2":{"name":'root2',"email":"root2@126.com"},
    "3":{"name":'root3',"email":"root3@126.com"},
    "4":{"name":'root4',"email":"root4@126.com"}
}

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

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

 4、detail.html的代码

<body>
    <h1>详细信息</h1>
    <h6>用户名:{{ detail_info.name }}</h6>
    <h6>邮箱:{{ detail_info.email }}</h6>
</body>

如图:

1、点击索引,进入

2、http://127.0.0.1:8000/detail/?nid=3 是以get方式请求

2.2、通过页面方式

说明:http://127.0.0.1:8000/detail-4.html  这种方式访问,以为是在一个页面上。

1、project.url.py配置

from django.contrib import admin
from django.urls import path,re_path
from app01 import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/',views.index),
    re_path(r'^detail-(\d+).html/',views.detail),  #django2.0以后用re_path支持正则,(\d+)传给后台视图函数,也是动态路由,一对多关系
]

 2、模板中index.html的代码

<body>
    <ul>
        {% for k,row in user_dict.items %}
            <li><a target="_blank" href="/detail-{{ k }}.html/">{{ row.name }}</a></li>
        {% endfor %}
    </ul>
</body>

3、app01.views.py

USER_DICT = {
    "1":{"name":'root1',"email":"root1@126.com"},
    "2":{"name":'root2',"email":"root2@126.com"},
    "3":{"name":'root3',"email":"root3@126.com"},
    "4":{"name":'root4',"email":"root4@126.com"}
}

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

def detail(request,nid): #接收前面url传过来的
    nid detail_info = USER_DICT.get(nid) 
    return render(request,'detail.html',{'detail_info':detail_info})

4、detail.html的代码

<body>
    <h1>详细信息</h1>
    <h6>用户名:{{ detail_info.name }}</h6>
    <h6>邮箱:{{ detail_info.email }}</h6>
</body>

 如图:

三、正则表达式分组

3.1、顺序传参

说明:如果是没有分组的话,是严格按照传入参数的顺序进行传值的。

url.py代码

from django.contrib import admin
from django.urls import path,re_path
from app01 import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/',views.index),
    re_path(r'^detail-(\d+)-(\d+).html/',views.detail),  #按照顺序向后台传入两个值
]

view.py代码

def detail(request,nid,uid):
    print(uid,nid)
    return  HttpResponse(nid)

 在浏览器输入:http://127.0.0.1:8000/detail-3-9.html/

3.2、分组形式传参

说明:如果是分组的话,是根据关键字传参。

url.py代码

from django.contrib import admin
from django.urls import path,re_path
from app01 import views

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r'^detail-(?P<nid>\d+)-(?P<uid>\d+).html/',views.detail),  #分组形式
]

view.py代码

def detail(request,nid,uid):
    print(uid,nid)
    return  HttpResponse(nid)

 在浏览器输入:http://127.0.0.1:8000/detail-3-9.html/

这种情况,传参数的时候,采用非固定参数的方式传参。所以view.py代码可以这样写:

def detail(request,*args,**kwargs):  #采用非固定参数接收参数
    print(kwargs)
    uid,nid = kwargs.get("nid"),kwargs.get("uid")
    print(uid,nid)
    return  HttpResponse(nid)

如图:

四、总结

路由系统:URL

  1. 普通url:re_path('^index/',view.index),re_path('^home/',view.Home.as_view())
  2. 顺序传参:re_path(r'^detail-(\d+)-(\d+).html/',views.detail),这个用*args接收
  3. 关键字传参:re_path(r'^detail-(?P<nid>\d+)-(?P<uid>\d+).html/',views.detail),这个用**kwargs接收,推荐使用这个

 

posted @ 2018-03-05 15:29  帅丶高高  阅读(282)  评论(0编辑  收藏  举报