简体   繁体   中英

Why won't this django-rest-swagger API documentation display/work properly?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM