defreckon(request, pg1, st, pg2): if st == 'add': k = pg1 + pg2 elif st == 'sub': k = pg1 - pg2 elif st == 'mul': k = pg1 * pg2 else: k = '错误!' html = f'<h1>{str(k)}</h1>' return HttpResponse(html)
re_path
解释一下这个正则表达式:
^ 代表开头
& 代表结束
P 代表正则匹配
< x > 代表我们给他命名为x
\d 匹配数字
{1,2} 匹配1-2位
\w+ 匹配字符串
defpage_2003_view(request): html = "<h1>这是我的第一个页面</h1>" return HttpResponse(html)
defpage_k(request): html = "<h1>这是我的首页</h1>" return HttpResponse(html)
defpage_1(request): html = "<h1>这是编号为1的页面</h1>" return HttpResponse(html)
defpage_2(request): html = "<h1>这是编号为2的页面</h1>" return HttpResponse(html)
defpage_view(request, pg): html = f"这是编号为{str(pg)}的页面" return HttpResponse(html)
defreckon(request, pg1, st, pg2): if st == 'add': k = pg1 + pg2 elif st == 'sub': k = pg1 - pg2 elif st == 'mul': k = pg1 * pg2 else: k = '错误!' html = f'<h1>{str(k)}</h1>' return HttpResponse(html)
defcal2_view(request,x,op,y): if op == 'add': k = int(x) + int(y) elif op == 'sub': k = int(x) - int(y) elif op == 'mul': k = int(x) * int(y) else: k = '错误!' html = f'<h1>cal2_view:{str(k)}</h1>' return HttpResponse(html)