1. pip install djangorestframework
  2. In your setting file, add the “rest_framework”:
    INSTALLED_APPS = (
    ...
    'rest_framework',
    )
    
  3. Create two files in your app:
    1. api/urls.py : define the url
    2. api/views.py: define the function
  4. In url.py: 
    re_path(r'^posts/hello/$', views.MyOwnView.as_view(), name='hello'),
  5. 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:

    http://www.django-rest-framework.org/api-guide/permissions/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.