I've built a Django API that when given an email address via POST will respond with a boolean value indicating weather or not that email address already exists in my database:
class isEmailTaken(views.APIView):
    permission_classes = [permissions.AllowAny,]
    def post(self, request, *args, **kwargs):
        try:
            email = request.DATA['email']
        except KeyError:
            return HttpResponse(
                'An email was not given with this request.',
                status=status.HTTP_400_BAD_REQUEST,
            )
        return HttpResponse(
            json.dumps(
                User.objects.filter(email=email),
                content_type="application/json",
                status=status.HTTP_200_OK,
            )
        )
Now I would like to use the django-rest-swagger package to automatically generate documentation for this API. I installed the package and inserted the comments you see above between the triple-quotes. When I look at the documentation produced by django-rest-swagger for this API, I see the image below.

However, when I click the Try it out! button, I get the error shown below. Notably, it never gives me a chance to input the email argument that it should send via POST.

Why doesn't the Django-Swagger-Package create docs that allow me to properly the argument "email" via POST? How do I make this work?
Use Swagger Inspector to quickly generate your OAS-based documentation for existing REST APIs by calling each end point and using the associated response to generate OAS-compliant documentation, or string together a series of calls to generate a full OAS document for multiple API endpoints.
I tested this with cigar_example which is made by django-rest-swagger and in that example they written one custom view which is also not rendering input parameters
Lastly i look into source code and found that django-rest-swagger needs get_serializer_class to build body parameters
So it worked with the following code:
class isEmailTaken(views.APIView):
    permission_classes = [permissions.AllowAny,]
    serializer_class = IsEmailTakenSerializer
    def get_serializer_class(self):
        return self.serializer_class
    def post(self, request, *args, **kwargs):
        try:
            email = request.DATA['email']
        except KeyError:
            return HttpResponse(
               'An email was not given with this request.', 
                status=status.HTTP_400_BAD_REQUEST,
            )
        return HttpResponse(
            json.dumps(
                User.objects.filter(email=email), 
                content_type="application/json",
                status=status.HTTP_200_OK,
             )
         )
and IsEmailTakenSerializer:
from rest_framework import serializers
class IsEmailTakenSerializer(serializers.Serializer):
    email = serializers.EmailField()
                        django-rest-swagger tries to send a POST request without some data.
First you have to fix your view like this:
from rest_framework import status
from django.http import HttpResponse
import json
def post(self, request, *args, **kwargs):
    try:
        email = request.DATA['email']
    except KeyError:
        return HttpResponse(
            'An email was not given with this request.', 
            status=status.HTTP_400_BAD_REQUEST,
        )
    return HttpResponse(
        json.dumps(
            User.objects.filter(email=email), 
            content_type="application/json",
            status=status.HTTP_200_OK,
        )
    )
If you try this, you should see your nice error message now.
Next step is to look at django-rest-swagger docs to find out what to do that it renders a html form field right above the "Try it out" button.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With