(编辑:jimmy 日期: 2024/11/14 浏览:2)
分组
分组的目的:让服务端获得url中的具体数据,通过分组,把需要的数据按函数传参的方式传递给服务器后台
1-无名分组
若要从URL 中捕获一个值,只需要在它周围放置一对圆括号
# app01/urls.py from django.urls import path, re_path from app01 import views app_name = "app01" urlpatterns = [ path("login/", views.login, name="Log"), re_path(r"articles/([0-9]{4})/([0-9]){2}/", views.articles), ]
# app01/views.py def articles(request, year, month ): print(year, month) return HttpResponse(year+"-"+month)
2-有名分组
在更高级的用法中,可以使用命名的正则表达式组来捕获URL 中的值并以关键字 参数传递给视图。
在Python 正则表达式中,命名正则表达式组的语法是("htmlcode">
# app01/urls.py from django.urls import path, re_path from app01 import views app_name = "app01" urlpatterns = [ path("login/", views.login, name="Log"), re_path(r"articles/(", views.articles), ]
捕获的值作为关键字参数而不是位置参数传递给视图函数
# app01/views.py def articles(request, month, year ): print(year, month) return HttpResponse(year+"-"+month)
分发
分发的目的:解决一个django项目中因为存在多个应用app导致project下面的urls臃肿和分配混乱的问题
分发的具体操作流程是:
step1:项目文件下的urls.py,使用include()
# project/urls.py from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path(r"app01/", include("app01.urls")), ]
step2:app下的具体url
# app01/urls.py from django.urls import path from app01 import views urlpatterns = [ path("login/", views.login), ]
step3:视图函数render时模板路径前缀
# app01/views.py from django.shortcuts import render # Create your views here. def login(request): return render(request, "app01/login.html")
step4:模板
# app01/templates/app01/login.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>app01_title</title> </head> <body> <h2>app01_login</h2> <hr> <form action="" method="post"> <p>用户名 <input type="text" name="name"></p> <p>密码 <input type="password" name="pwd"></p> <p><input type="submit" value="登录"></p> </form> </body> </html>
反向解析
反向解析的目的:解决url硬编码的问题,即不能写死一个url,否则日后修改url,造成的维护成本巨大
此时可以给url命名,然后可以在视图和模板中使用url别名,反向解析出正式的url
反向解析分两种:模板中解析、视图中解析
模板中解析 <form action="{% url "Art" 12 %}" method="post">
# app01/urls.py from django.urls import path from app01 import views urlpatterns = [ path("login/", views.login, name="Log"), path("articles/<int:id>/", views.articles, name="Art"), #有参 ]
# app01/templates/app01/login.html <form action="{% url "Log" %}" method="post"> <p>用户名 <input type="text" name="name"></p> <p>密码 <input type="password" name="pwd"></p> <p><input type="submit" value="登录"></p> </form> <form action="{% url "Art" 12 %}" method="post"> <p>用户名 <input type="text" name="name"></p> <p>密码 <input type="password" name="pwd"></p> <p><input type="submit" value="登录"></p> </form>
视图中解析 reverse("Art", args=(id,))
# app01/views.py from django.shortcuts import render from django.urls import reverse # Create your views here. def login(request): print(reverse('log')) id=10 print(reverse("Art", args=(id,))) return render(request, "app01/login.html")
命名空间
命名空间要配合反向解析使用,当存在多个app,url的name冲突时,需要指定该name的命名空间
# project/urls.py from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path(r"app01/", include("app01.urls", namespace="app01")), path(r"app02/", include("app02.urls", namespace="app02")), ]
在django2.x中,app01/url.py需要添加 app_name = "app01"
# app01/urls.py from django.urls import path from app01 import views app_name = "app01" urlpatterns = [ path("login/", views.login, name="Log"), ]
# app01/views.py(需要时设置) from django.shortcuts import render from django.urls import reverse # Create your views here. def login(request): a = reverse("app01:Log") print("app01:", a) return render(request, "app01/login.html")
# app01/templates/app01/login.html(需要时设置) <form action="{% url "app01:Log" %}" method="post"> <p>用户名 <input type="text" name="name"></p> <p>密码 <input type="password" name="pwd"></p> <p><input type="submit" value="登录"></p> </form>
转换器
对于django2.0版本以后,出现新的path() urlconf,他有两个好处:
1:url修改维护变得简单
2:url获得的参数的数据类型不再是单纯的str,在path中可以通过转换器实现参数数据类型的转换
path()中存在5个内置转化器:
=1. str,匹配除了路径分隔符(/)之外的非空字符串,这是默认的形式
2. int,匹配正整数,包含0。
3. slug,匹配字母、数字以及横杠、下划线组成的字符串。
4. uuid,匹配格式化的uuid,如 075194d3-6885-417e-a8a8-6c931e272f00。
5. path,匹配任何非空字符串,包含了路径分隔符
使用方法:
step1: app01/urls.py
from django.urls import path, re_path from app01 import views app_name = "app01" urlpatterns = [ path("login/", views.login, name="Log"), # re_path(r"articles/(", views.articles), path("articles/<int:year>/<int:month>/", views.articles), ]
step2: app01/views.py
def articles(request, year, month): print(year, month) print(type(year)) return HttpResponse("ok")
class YearConverter: regex = '[0-9]{4}' def to_python(self, value): return int(value) def to_url(self, value): return '%04d' % value
step2: app01/urls.py
from django.urls import path, re_path, register_converter from app01 import views, my_converters register_converter(my_converters.YearConverter, 'year') app_name = "app01" urlpatterns = [ path("login/", views.login, name="Log"), # re_path(r"articles/(", views.articles), # path("articles/<int:year>/<int:month>/", views.articles), path("books/<year:y>", views.books), ]
step3: app01/views.py
def books(request, y): print(y, type(y)) return HttpResponse("ok,books")
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。