diff --git a/api_tut/settings.py b/api_tut/settings.py index 1d6c19f..4041cce 100644 --- a/api_tut/settings.py +++ b/api_tut/settings.py @@ -27,6 +27,9 @@ ALLOWED_HOSTS = [] +APPEND_SLASH=False +CORS_ALLOW_ALL_ORIGINS = True + # Application definition @@ -37,7 +40,9 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'first_app.apps.FirstAppConfig' + 'first_app.apps.FirstAppConfig', + 'corsheaders', + ] MIDDLEWARE = [ @@ -48,6 +53,8 @@ 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'corsheaders.middleware.CorsMiddleware', + 'django.middleware.common.CommonMiddleware', ] ROOT_URLCONF = 'api_tut.urls' diff --git a/first_app/urls.py b/first_app/urls.py index 3fd99c8..4d4d0c1 100644 --- a/first_app/urls.py +++ b/first_app/urls.py @@ -4,4 +4,5 @@ urlpatterns = [ path('', views.home), path('first', views.first), + path('multiply', views.multiply), ] diff --git a/first_app/views.py b/first_app/views.py index 5c81a81..6e6e014 100644 --- a/first_app/views.py +++ b/first_app/views.py @@ -1,9 +1,35 @@ from django.shortcuts import render from django.http import JsonResponse +from django.http import HttpResponse + def first(request): return JsonResponse({"data":7}) def home(request): print(request) - return JsonResponse({"data":7}) \ No newline at end of file + return JsonResponse({"data":7}) + +def multiply(request): + #print(request) + if request.method != 'GET': + return JsonResponse({"answer":'Please send Get request!'}) + + listOfVal= list(request.GET.dict().values()) + if len(listOfVal)!=2: + return JsonResponse({"answer":'Please send exactly 2 params!'}) + + a=listOfVal[0] + b=listOfVal[1] + if isfloat(a) and isfloat(b): + return JsonResponse({"answer":str(float(a)*float(b))}) + return JsonResponse({"answer":'Please send numbsers!'}) + + + +def isfloat(value): + try: + float(value) + return True + except ValueError: + return False \ No newline at end of file