Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- КОД В /operations/urls.py
- ---------------------------------------------------------------------------------------
- from django.urls import path, re_path
- from . import views
- urlpatterns = [
- path('', views.index, name='index'),
- # path('add/<int:arg_1>/<int:arg_2>/', views.add, name='add'),
- # path('divide/<int:arg_1>/<int:arg_2>/', views.divide, name='divide'),
- # path('multiplication/<int:arg_1>/<int:arg_2>/', views.multiplication, name='multiplication'),
- # path('subtraction/<int:arg_1>/<int:arg_2>/', views.subtraction, name='subtraction'),
- re_path('add/(?P<arg_1>\d+.?\d*)\/(?P<arg_2>\d+.?\d*)/', views.add, name='add'),
- re_path('divide/(?P<arg_1>\d+.?\d*)\/(?P<arg_2>\d+.?\d*)/', views.divide, name='divide'),
- re_path('multiplication/(?P<arg_1>\d+.?\d*)\/(?P<arg_2>\d+.?\d*)/', views.multiplication, name='multiplication'),
- re_path('subtraction/(?P<arg_1>\d+.?\d*)\/(?P<arg_2>\d+.?\d*)/', views.subtraction, name='subtraction')
- ]
- -----------------------------------------------------------------------------------------
- КОД В /operations/views.py
- -----------------------------------------------------------------------------------------
- from django.shortcuts import render
- from django.http import HttpResponse
- # Create your views here.
- def if_int(num):
- if num == int(num):
- return int(num)
- else:
- return num
- def index(request):
- return HttpResponse('Hello World!')
- def add(request, arg_1, arg_2):
- result = float(arg_1) + float(arg_2)
- result = if_int(result)
- return HttpResponse(result)
- def divide(request, arg_1, arg_2):
- try:
- result = float(arg_1) / float(arg_2)
- result = if_int(result)
- return HttpResponse(result)
- except:
- return HttpResponse('You can\'t divide by zero!')
- def multiplication(request, arg_1, arg_2):
- result = float(arg_1) * float(arg_2)
- result = if_int(result)
- return HttpResponse(result)
- def subtraction(request, arg_1, arg_2):
- result = float(arg_1) - float(arg_2)
- result = if_int(result)
- return HttpResponse(result)
- -------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment