文章目录
参考资料
达内教育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))