sredo

project "float calc"

Apr 4th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. КОД В /operations/urls.py
  2. ---------------------------------------------------------------------------------------
  3.  
  4. from django.urls import path, re_path
  5. from . import views
  6.  
  7.  
  8. urlpatterns = [
  9.     path('', views.index, name='index'),
  10.     # path('add/<int:arg_1>/<int:arg_2>/', views.add, name='add'),
  11.     # path('divide/<int:arg_1>/<int:arg_2>/', views.divide, name='divide'),
  12.     # path('multiplication/<int:arg_1>/<int:arg_2>/', views.multiplication, name='multiplication'),
  13.     # path('subtraction/<int:arg_1>/<int:arg_2>/', views.subtraction, name='subtraction'),
  14.     re_path('add/(?P<arg_1>\d+.?\d*)\/(?P<arg_2>\d+.?\d*)/', views.add, name='add'),
  15.     re_path('divide/(?P<arg_1>\d+.?\d*)\/(?P<arg_2>\d+.?\d*)/', views.divide, name='divide'),
  16.     re_path('multiplication/(?P<arg_1>\d+.?\d*)\/(?P<arg_2>\d+.?\d*)/', views.multiplication, name='multiplication'),
  17.     re_path('subtraction/(?P<arg_1>\d+.?\d*)\/(?P<arg_2>\d+.?\d*)/', views.subtraction, name='subtraction')
  18. ]
  19.  
  20. -----------------------------------------------------------------------------------------
  21.  
  22. КОД В /operations/views.py
  23. -----------------------------------------------------------------------------------------
  24.  
  25. from django.shortcuts import render
  26. from django.http import HttpResponse
  27.  
  28. # Create your views here.
  29.  
  30.  
  31. def if_int(num):
  32.     if num == int(num):
  33.         return int(num)
  34.     else:
  35.         return num
  36.  
  37.  
  38. def index(request):
  39.     return HttpResponse('Hello World!')
  40.  
  41.  
  42. def add(request, arg_1, arg_2):
  43.     result = float(arg_1) + float(arg_2)
  44.     result = if_int(result)
  45.     return HttpResponse(result)
  46.  
  47.  
  48. def divide(request, arg_1, arg_2):
  49.     try:
  50.         result = float(arg_1) / float(arg_2)
  51.         result = if_int(result)
  52.         return HttpResponse(result)
  53.     except:
  54.         return HttpResponse('You can\'t divide by zero!')
  55.  
  56.  
  57. def multiplication(request, arg_1, arg_2):
  58.     result = float(arg_1) * float(arg_2)
  59.     result = if_int(result)
  60.     return HttpResponse(result)
  61.  
  62.  
  63. def subtraction(request, arg_1, arg_2):
  64.     result = float(arg_1) - float(arg_2)
  65.     result = if_int(result)
  66.     return HttpResponse(result)
  67.  
  68.  
  69. -------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment