Step 1: Download Python using the below Url
Step 2: To Install django using command prompt
Step 3: To Install django rest framework using command prompt
Step 4:To create default project template
Step 5:Navigate into <ProjectName> folder. ex: cd ProjectName
Step 6:To create API project template
Step 7:Open settings.py file under "INSTALLED_APPS" add the below code "rest_framework" and ApiProjectName.
Step 8:Open view.py file inside ApiProjectName folder add the below lines.
Step 9: Open urls.py file add below code
Step 2: To Install django using command prompt
pip install django
Step 3: To Install django rest framework using command prompt
pip install djangorestframework
Step 4:To create default project template
django-admin startproject <ProjectName>
Step 5:Navigate into <ProjectName> folder. ex: cd ProjectName
Step 6:To create API project template
python manage.py startapp <ApiProjectName>
Step 7:Open settings.py file under "INSTALLED_APPS" add the below code "rest_framework" and ApiProjectName.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'ApiProjectName',
]
Step 8:Open view.py file inside ApiProjectName folder add the below lines.
from django.http import Http404 from rest_framework.views import APIView from rest_framework.decorators import api_view from rest_framework.response import Response from rest_framework import status from django.http import JsonResponse from django.core import serializers from django.conf import settings import json
@api_view(["POST"]) def SquareNumber(requestNumber): try: value=json.loads(requestNumber.body) result=str(value*value) return JsonResponse("Square of "+str(value)+" is "+result,safe=False) except ValueError as e: return Response(e.args[0],status.HTTP_400_BAD_REQUEST)
Step 9: Open urls.py file add below code
from django.conf.urls import url
from django.contrib import admin
from ApiProjectName import views
urlpatterns = [
url('admin/', admin.site.urls),
url('square/',views.SquareNumber),
]
Step 10: To run to application, use the below command in command prompt.python manage.py runserver
Step 11: Open postman and create request as POST and url as http://127.0.0.1:8000/square and click body tab and choose Raw and JSON as type and in body content as some value ex: 10. and click and send button.
No comments:
Post a Comment