一.正则url(解决正则部分不能被匹配的问题)
-------xxx.html<a href="/teacher_edit-{{ item.id }}">编辑</a>-------url.pypath(‘teacher_edit-(\d+)‘,views.teacher_edit),--------views.pydef teacher_edit(req): xxxx###########################以上的正则url不能被识别,解决方法如下:----------------urls.pyfrom django.urls import path,re_path ????????????????#引入re_path模块re_path(‘teacher_edit-(\d+)‘,views.teacher_edit), ???#此处有一个正则部分就是(\d+),所以也就是有一个参数传给teacher_edit()-----------------views.pydef teacher_edit(req,edit): ?????????????????????????#参数edit是urls.py中的正则部分 print(edit)
二.模板url
----------index.html<a href="{% url "A" B C %}">ABC</a> #找到urls.py中name="A"的path(),path()中的url跟A无关----------urls.pypath("(\w+)/(\w+)",views.index,name="A") ?# 此处只匹配A-----------views.pydef index(req,B,C): ?????????#B,C参数是path()中url的正则部分 ???return render(req,"index.html")
Django(模板url+正则url)
原文地址:https://www.cnblogs.com/gaoyukun/p/9220189.html