Django后端开发——路由配置(三) re_path( )


参考资料

达内教育Django后端开发网课:b站网址
本文为后端学习的自用笔记


re_path()

在这里插入图片描述

示例:两位数计算器

仅进行两位数的运算

urls.py的修改

from django.contrib import admin
from django.urls import path,re_path
from . import views
urlpatterns = [
    path('admin/', admin.site.urls),

    #http://127.0.0.1:8000/page/2003
    path('page/2003/', views.page_2003_view),

    #path('page/2003/', views.page_2003),

    #http://127.0.0.1:8000/
    path('',views.index),
    #http://127.0.0.1:8000/page/1
    path('page/1', views.page_1_view),
    #http://127.0.0.1:8000/page/2
    path('page/2', views.page_2_view),
    #http://127.0.0.1:8000/page/3~n
    path('page/<int:pg>',views.pagen_view),

    #http://127.0.0.1:8000/2位整数/操作符/2位整数
    re_path(r'^(?P<x>\d{1,2})/(?P<op>\w+)/(?P<y>\d{1,2})$',views.cal2_view),
    #\d{1,2}表示一到两位整数  \w+表示多个字符

    #http://127.0.0.1:8000/整数/操作符/整数
    path('<int:n>/<str:op>/<int:m>',views.cal_view),

]

添加内容:

    #http://127.0.0.1:8000/2位整数/操作符/2位整数
    re_path(r'^(?P<x>\d{1,2})/(?P<op>\w+)/(?P<y>\d{1,2})$',views.cal2_view),
    #\d{1,2}表示一到两位整数  \w+表示多个字符

views.py的修改

from django.http import  HttpResponse


def page_2003_view(request):

    html = "<h1>this is the first page</h1>"
    #<h1>是控制字体大小的
    return HttpResponse(html)

def index(request):
    html = '这是首页'
    return HttpResponse(html)
def page_1_view(request):
    html = '这是第1页'
    return HttpResponse(html)

def page_2_view(request):
    html = '这是第2页'
    return HttpResponse(html)

def pagen_view(request,pg):
    html = '这是第%s页'%(pg)
    return HttpResponse(html)

def cal_view(request,n,op,m):
    if op not in ['add','sub','mul']:
        return HttpResponse('your op is wrong')
    result=0
    if op=='add':
        result=n+m
    elif op=='sub':
        result=n-m
    elif op=='mul':
        result=n*m
    return HttpResponse('结果为:%s'%(result))

def cal2_view(request,x,op,y):
    html='x:%s op:%s y:%s'%(x,op,y)
    return HttpResponse(html)

添加内容:

    def cal2_view(request,x,op,y):
    html='x:%s op:%s y:%s'%(x,op,y)
    return HttpResponse(html)

这样的修改并不能做到只运算两位数,但是可以区分需要计算的两个数字是否均为两位数

效果

全是两位数:
在这里插入图片描述
不全是两位数:
在这里插入图片描述


练习

在这里插入图片描述

做法1

两种情况分别交由两个view,需要定义两种view

urls.py的修改

from django.contrib import admin
from django.urls import path,re_path
from . import views
urlpatterns = [
    path('admin/', admin.site.urls),

    #http://127.0.0.1:8000/page/2003
    path('page/2003/', views.page_2003_view),

    #path('page/2003/', views.page_2003),

    #http://127.0.0.1:8000/
    path('',views.index),
    #http://127.0.0.1:8000/page/1
    path('page/1', views.page_1_view),
    #http://127.0.0.1:8000/page/2
    path('page/2', views.page_2_view),
    #http://127.0.0.1:8000/page/3~n
    path('page/<int:pg>',views.pagen_view),

    #http://127.0.0.1:8000/2位整数/操作符/2位整数
    re_path(r'^(?P<x>\d{1,2})/(?P<op>\w+)/(?P<y>\d{1,2})$',views.cal2_view),
    #\d{1,2}表示一到两位整数  \w+表示多个字符

    #http://127.0.0.1:8000/整数/操作符/整数
    path('<int:n>/<str:op>/<int:m>',views.cal_view),

    #http://127.0.0.1:8000/birthday/四位整数/二位整数/二位整数
    re_path(r'^birthday/(?P<m>\d{4})/(?P<n>\d{1,2})/(?P<t>\d{1,2})$',views.birth1_view),
    # http://127.0.0.1:8000/birthday/二位整数/二位整数/四位整数
    re_path(r'^birthday/(?P<m>\d{1,2})/(?P<n>\d{1,2})/(?P<t>\d{4})$', views.birth2_view)
]

添加内容:

    #http://127.0.0.1:8000/birthday/四位整数/二位整数/二位整数
    re_path(r'^birthday/(?P<m>\d{4})/(?P<n>\d{1,2})/(?P<t>\d{1,2})$',views.birth1_view),
    # http://127.0.0.1:8000/birthday/二位整数/二位整数/四位整数
    re_path(r'^birthday/(?P<m>\d{1,2})/(?P<n>\d{1,2})/(?P<t>\d{4})$', views.birth2_view)

views.py的修改

from django.http import  HttpResponse


def page_2003_view(request):

    html = "<h1>this is the first page</h1>"
    #<h1>是控制字体大小的
    return HttpResponse(html)

def index(request):
    html = '这是首页'
    return HttpResponse(html)
def page_1_view(request):
    html = '这是第1页'
    return HttpResponse(html)

def page_2_view(request):
    html = '这是第2页'
    return HttpResponse(html)

def pagen_view(request,pg):
    html = '这是第%s页'%(pg)
    return HttpResponse(html)

def cal_view(request,n,op,m):
    if op not in ['add','sub','mul']:
        return HttpResponse('your op is wrong')
    result=0
    if op=='add':
        result=n+m
    elif op=='sub':
        result=n-m
    elif op=='mul':
        result=n*m
    return HttpResponse('结果为:%s'%(result))

def cal2_view(request,x,op,y):
    html='x:%s op:%s y:%s'%(x,op,y)
    return HttpResponse(html)

def birth1_view(request,m,n,t):
    return HttpResponse('生日为:%s年%s月%s日'%(m,n,t))
def birth2_view(request,m,n,t):
    return HttpResponse('生日为:%s年%s月%s日'%(t,m,n))

添加内容:

def birth1_view(request,m,n,t):
    return HttpResponse('生日为:%s年%s月%s日'%(m,n,t))
def birth2_view(request,m,n,t):
    return HttpResponse('生日为:%s年%s月%s日'%(t,m,n))

效果

在这里插入图片描述
在这里插入图片描述

做法2

两种情况都交由同一个view处理

urls.py的修改

添加内容:

    #http://127.0.0.1:8000/birthday/四位整数/二位整数/二位整数
    re_path(r'^birthday/(?P<y>\d{4})/(?P<m>\d{1,2})/(?P<d>\d{1,2})$',views.birth_view),
    # http://127.0.0.1:8000/birthday/二位整数/二位整数/四位整数
    re_path(r'^birthday/(?P<m>\d{1,2})/(?P<d>\d{1,2})/(?P<y>\d{4})$',views.birth_view)

views.py的修改

添加内容:

    def birth_view(request,y,m,d):
    return HttpResponse('生日为:%s年%s月%s日'%(y,m,d))

  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

^_^2412

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值