前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >django-URL转换器(四)

django-URL转换器(四)

作者头像
西西嘛呦
发布2020-08-26 10:20:29
5280
发布2020-08-26 10:20:29
举报

接URL匹配那一节。

在book中的urls.py

代码语言:javascript
复制
from django.urls import path
from . import views

urlpatterns = [
    path('', views.index),
    path('web/', views.web),
    path('19/0425/<html>',views.trans),
]

在book中的views.py

代码语言:javascript
复制
from django.http import 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--匹配的一个非空字符串,包括‘/’;

代码语言:javascript
复制
from django.urls import path
from . import views

urlpatterns = [
    path('', views.index),
    path('web/', views.web),
    path('19/0425/<str:html>',views.trans),
    path('19/0425/<int:page>',views.trans2),
    path('19/0425/<int:numa>/<int:numb>',views.trans3),
    path('19/0425/<slug:slugStr>',views.trans4),
    path('19/0425/<uuid:uu>',views.trans5),
    path('19/0425/<path:home>',views.trans6),
]
代码语言:javascript
复制
from django.http import HttpResponse


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

def web(request):
    html="<h1>Djang Web</h1>"
    return HttpResponse(html)

def trans(request,html):
    return HttpResponse("<h1>{}</h1>".format(html))

def trans2(request,page):
    return HttpResponse("<h1>{}</h1>".format(page))

def trans3(request,numa,numb):
    return HttpResponse("<h1>{}</h1>".format(numa+numb))

def trans4(request,slugStr):
    return HttpResponse("<h1>{}</h1>".format(slugStr))

def trans5(request,uu):
    return HttpResponse("<h1>{}</h1>".format(uu))

def trans6(request,home):
    return HttpResponse("<h1>{}</h1>".format(home))
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档