微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

django-URL转换器四

接URL匹配那一节。

在book中的urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('',views.index),path('web/'19/0425/<html>
from django.http  HttpResponse

#html参数用来接收urls.py中的<html>
def trans(request,html): return HttpResponse("<h1>{}</h1>".format(html))

启动服务后,我们在浏览器中输入http://127.0.0.1:8000/19/0425/transform

即urls.py中的html参数为transform,成功的显示在了前端界面中。

一般有五种方式:
(1)str--匹配的第一个非空字符,除去‘/’,认使用的是这种方式;

(2)int--匹配0或正整数;

 

 

(3)slug--由ASCII字母或数字组成,通过'-'连接的字符串;

(4)uuid--uuid格式的字符串;

(5)path--匹配的一个非空字符串,包括‘/’;

 path
19/0425/<str:html>19/0425/<int:page>19/0425/<int:numa>/<int:numb>19/0425/<slug:slugStr>19/0425/<uuid:uu>19/0425/<path:home>
 HttpResponse


#157f572e-ebbc-4914-a5b2-6e3ed0bb9c8c
# Create your views here.
 index(request):
    html=<h1 style='color:red'>hello world</h1>"
    return HttpResponse(html)

 web(request):
    html=<h1>Djang Web</h1>".format(html))

 trans2(request,page):
    .format(page))

 trans3(request,numa,numb):
    ".format(numa+numb))

 trans4(request,slugStr):
    .format(slugStr))

 trans5(request,uu):
    .format(uu))

 trans6(request,home):
    ".format(home))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐