Django练习

url传参实现加减乘除

1,urls里编写path

 path('computer/<int:num1>/<str:op>/<int:num2>/', views.get_result_view),

2,views里编写

def get_result_view(request , num1 , op , num2):
    oplist = ['+','_','*','div']
    # op是否在list里
    result = 0
    if op in oplist:
        if op == "+":
            result = num1 + num2
        elif op== "-":
            result = num1 - num2
        elif op == "*":
            result = num1 * num2
        elif op == "div":
            result = num1 / num2
        else:
            result = 0
        return HttpResponse("%d %s %d=%d" %(num1,op,num2,result ))
    else:
        return HttpResponse("Error运算符不符合规范,请在以下运算符中选取+,-,*,/")

3,启动

python manage.py runserver

4,网页验证

127.0.0.1:8000/hello/computer/10/*/10/

Django练习

 

 担心/会和路径冲突,改成div

Django练习

 

 

每日博客

上一篇:方法


下一篇:Codeforces Round #754 (Div. 2) A - A.M. Deviation