Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion api_tut/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

ALLOWED_HOSTS = []

APPEND_SLASH=False
CORS_ALLOW_ALL_ORIGINS = True


# Application definition

Expand All @@ -37,7 +40,9 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'first_app.apps.FirstAppConfig'
'first_app.apps.FirstAppConfig',
'corsheaders',

]

MIDDLEWARE = [
Expand All @@ -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'
Expand Down
1 change: 1 addition & 0 deletions first_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
urlpatterns = [
path('', views.home),
path('first', views.first),
path('multiply', views.multiply),
]
28 changes: 27 additions & 1 deletion first_app/views.py
Original file line number Diff line number Diff line change
@@ -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})
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