The easiest restapi for Django
- pip install djangorestframework
- In your setting file, add the “rest_framework”:
INSTALLED_APPS = ( ... 'rest_framework', )
- Create two files in your app:
- api/urls.py : define the url
- api/views.py: define the function
- In url.py:
re_path(r'^posts/hello/$', views.MyOwnView.as_view(), name='hello'),
- In views.py:
from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.permissions import IsAuthenticated, AllowAny class MyOwnView(APIView): permission_classes = (AllowAny,) def get(self, request): myDict = {"first": "one", "second": "two"} return Response(myDict)
You can also use decorators, it will be simpler: