1.图书管理系统中使用分组或命名分组、URL的命名和反向解析;
2.删除功能三合一;
完成后可预习ORM相关博客:
https://www.cnblogs.com/maple-shaw/articles/9323320.html
https://www.cnblogs.com/maple-shaw/articles/9403501.html
urls.py文件
from app_book import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^del_(publisher|author|book)/(\d+)/', views.delete), #分组或命名分组
url(r'^publisher_list/', views.publisher_list,name='publisher'),
url(r'^add_publisher/', views.AddPuublisher.as_view()),
# url(r'^del_publisher/', views.del_publisher),
url(r'^edit_publisher/', views.edit_publisher),
url(r'^index/', views.index),
删除
view.py文件
def delete(request,table,pk):
#table='del_(publisher|author|book)',pk=(\d+)
# href="/del_book/{{book.pk}}/"
print(table,pk)
# 反射,查找对应的对象
table_class=getattr(models,table.capitalize())
table_class.objects.filter(pk=pk).delete() #删除
return redirect(reverse(table)) #重定向到对应的展示页面,反向解析
<td>
<a class="btn btn-success btn-xs" href="/edit_author/?id={{author.pk}}">编辑</a> <a class="btn btn-danger btn-xs" href="/del_author/{{author.pk}}/" >删除作者</a> <a class="btn btn-primary btn-xs" href="/add_author/">新增作者</a>
</td>
删除作者利用的就是分组匹配