(2)正则路径——无名分组
- 使用正则路径,需要使用
re_path
方法,同样通过name='别名'
配置路径别名,实例:
- 修改urls.py文件 #-*- coding: utf-8 -*- from django.urls import path,re_path from . import views urlpatterns = [ path('index/',views.index), re_path('^login/([0-9]{4})/$',views.login,name="login"), ] - 修改views.py # -*- coding: utf-8 -*- from django.urls import reverse from django.http import HttpResponse from django.shortcuts import redirect,render from django.views.decorators.csrf import csrf_exempt @csrf_exempt def index(request): if request.method == "GET": return HttpResponse("请使用POST") else: username = request.POST.get('username') pwd = request.POST.get('pwd') if username == 'zhangsan' and pwd == '123456': return HttpResponse("登录成功!!") else: return redirect(reverse('login',args=('2022',))) @csrf_exempt def login(request,year): #添加一个参数 print(year) return HttpResponse('Test')
- 使用
postman
访问127.0.0.1:8000/index
,观察终端
(3)正则路径——无名分组
- 实例:
- 修改urls.py文件 #-*- coding: utf-8 -*- from django.urls import path,re_path from . import views urlpatterns = [ path('index/',views.index), re_path('^login/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$',views.login,name="login"), ] - 修改views.py文件 # -*- coding: utf-8 -*- from django.urls import reverse from django.http import HttpResponse from django.shortcuts import redirect,render from django.views.decorators.csrf import csrf_exempt @csrf_exempt def index(request): if request.method == "GET": return HttpResponse("请使用POST") else: username = request.POST.get('username') pwd = request.POST.get('pwd') if username == 'zhangsan' and pwd == '123456': return HttpResponse("登录成功!!") else: return redirect(reverse('login',kwargs={"year":"2022","month":"09"})) @csrf_exempt def login(request,year,month): print(year + month) return HttpResponse('Test')
使用postman
访问127.0.0.1:8000/index
,观察终端
四、反向解析(使用模板)
(1)普通路径
- 在模板文件(html文件)中使用反向解析,利用
{% url '别名' %}
- urls.py文件
#-*- coding: utf-8 -*- from django.urls import path from . import views urlpatterns = [ path('index/',views.index), path('login/',views.login,name="login"), #别名login ]
- views.py文件
# -*- coding: utf-8 -*- from django.urls import reverse from django.http import HttpResponse from django.shortcuts import redirect,render from django.views.decorators.csrf import csrf_exempt def index(request): #index资源返回html页面,页面使用post方法反向解析到login资源 return render(request,"test.html") @csrf_exempt #因为html使用post方法,所以需要使用装饰器将此函数可以接受post方式 def login(request): username = request.POST.get('username') pwd = request.POST.get('pwd') if username == "zhangsan" and pwd == "123456": return HttpResponse("登录成功") else: return HttpResponse("登录失败")
- templates/test.html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h3>用户登录</h3> <form action="{% url 'login' %}" method="post"> {% csrf_token %} #使用post方式,需要添加 <p>用户名:<input type="text" name="username"></p> <p>密码:<input type="text" name="pwd"></p> <input type="submit"> </form> </body> </html>
- 使用浏览器访问
127.0.0.1:8000/index/
,先输出正确的username
和pwd
,查看终端输出,然后输出错误的参数
(2)正则路径——无名分组
- 使用正则路径时,模板中利用
{% url "别名" 符合正则匹配的参数 %}
来实现反向解析 - urls.py文件
#-*- coding: utf-8 -*- from django.urls import path,re_path from . import views urlpatterns = [ path('index/',views.index), re_path('^login/([0-9]{4})/$',views.login,name="login"), #别名login ]
- views.py文件
# -*- coding: utf-8 -*- from django.urls import reverse from django.http import HttpResponse from django.shortcuts import redirect,render from django.views.decorators.csrf import csrf_exempt def index(request): return render(request,"test.html") @csrf_exempt def login(request,year): #增加参数year username = request.POST.get('username') pwd = request.POST.get('pwd') if username == "zhangsan" and pwd == "123456": return HttpResponse("登录成功,当前年份:" + year) else: return HttpResponse("登录失败,当前年份:" + year)
- templates/test.html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h3>用户登录</h3> <form action="{% url 'login' '2022' %}" method="post"> #无名分组,添加参数 {% csrf_token %} <p>用户名:<input type="text" name="username"></p> <p>密码:<input type="text" name="pwd"></p> <input type="submit"> </form> </body> </html>
- 访问
127.0.0.1:8000/index/
,使用正确的username
和pwd
,查看终端输出,然后使用错误的参数进行访问