一 路由系统进阶(urls.py)

 动态路由

urls.py中通过正则表达式的分组匹配,捕获用户访问的url中的值,传递给视图函数
1 分组匹配(通过圆括号):
相当于给视图函数传递 位置参数

例子:

 1 from django.conf.urls import url
 2 
 3 from . import views
 4 
 5 urlpatterns = [
 6     url(r'^articles/2003/$', views.special_case_2003),
 7     url(r'^articles/([0-9]{4})/$', views.year_archive),
 8     url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),
 9     url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
10 ]

 

2 分组命名匹配:
相当于给视图函数传递 关键字参数

在Python的正则表达式中,分组命名正则表达式组的语法是(?P<name>pattern),其中name是组的名称,pattern是要匹配的模式。

例子:

 1 from django.conf.urls import url
 2 
 3 from . import views
 4 
 5 urlpatterns = [
 6     url(r'^articles/2003/$', views.special_case_2003),
 7     url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
 8     url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),
 9     url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.article_detail),
10 ]

3 name

防止将url硬编码到我们的业务逻辑代码中,给url起别名
通过别名,反向找到 url

配置:

在views.py中:
from django.urls import reverse
具体的url = reverse('url别名')

例子:

urls.py里面配置:

url(r'^publisher_list/$', views.publisher_list, name="alex")

vivews.py引用:

# def edit_publisher(request, edit_id):
#     print(reverse('alex'))
#     print("=" * 120)
#     if request.method == "POST":
#         new_name = request.POST.get("name888")
#         # 去数据库修改出版社名字
#         obj = models.Publisher.objects.get(id=edit_id)
#         obj.name = new_name
#         obj.save()
#         return redirect(reverse('alex')) #返回一个url
#     print(edit_id)
#     publisher_obj = models.Publisher.objects.get(id=edit_id)
#     return render(request, "edit_publisher.html", {"obj": publisher_obj})

4 传参数的两种写法(一不小心就被坑了)

例子一:url传参

1 urls.py配置:

 url(r'^edit_publisher/$', views.edit_publisher),

2 views.py 

 3 def edit_publisher(request):
    if request.method=="POST": 4 #获取用户更改的id 5 edit_id=request.POST.get("id")#从浏览器传的参数获取的id 6 new_name=request.POST.get("name")#从form表单获取的名字 7 #去数据库找到这条记录 8 obj=models.Publisher.objects.get(id=edit_id) 9 obj.name=new_name 10 obj.save() 11 return redirect("/publisher_list/") 12 else: 13 edit_id = request.GET.get("id") 14 publisher_edit = models.Publisher.objects.get(id=edit_id) 15 return render(request,"edit_publisher.html",{"obj":publisher_edit})

注意:#上面红色字体里面的的obj一定要和你相应的edit_publisher里面的value,里面的一致,比如value=obj.name 这里就一定用obj

3 html配置:

publisher_list.html

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>出版社列表</title>
 6     <link rel="stylesheet" href="/static/bootstrap-3.3.7/css/bootstrap.min.css">
 7 </head>
 8 <body>
 9 <div class="container">
10     <div class="row">
11         <div class="col-md-8 col-sm-offset-2">
12             <table class="table table-bordered">
13                 <tr>
14                     <th>#</th>
15                     <th>id</th>
16                     <th>出版社名字</th>
17                     <th>操作</th>
18                 </tr>
19 
20 {#                 data这里一定要跟views里面的data一样#}
21                 {% for publisher in data %}
22                     <tr>
23                     <td>{{ forloop.counter }}</td>
24                     <td>{{ publisher.id }}</td>
25                     <td>{{ publisher.name }}</td>
26                     <td>
27 {#                           这是动态传参#}
28 {#                        <a href="/edit_publisher/{{ publisher.id }}/" class="btn btn-info">编辑</a>#}
29 {#                        这是浏览器传参数#}
30                         <a href="/edit_publisher/?id={{ publisher.id }}" class="btn btn-info">编辑</a>
31                         <a href="/del_publisher" class="btn btn-info">删除</a>
32                     </td>
33                     </tr>
34                 {% endfor %}
35             </table>
36 
37         </div>
38     </div>
39 </div>
40 
41 </body>
42 </html>
View Code

相关文章: