为什么这个django-rest-swagger API文档不会显示/正常工作? [英] Why won't this django-rest-swagger API documentation display/work properly?

查看:1544
本文介绍了为什么这个django-rest-swagger API文档不会显示/正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个Django API,当通过POST给出一个电子邮件地址时,将使用一个布尔值来表示天气,或者该电子邮件地址已经存在于我的数据库中:

  class isEmailTaken(views.APIView):
permission_classes = [permissions.AllowAny,]

def post(self,request,* args,* * b $ b try:
email = request.DATA ['email']
除了KeyError:
return HttpResponse(
'这个电子邮件没有给出请求',
status = status.HTTP_400_BAD_REQUEST,

返回HttpResponse(
json.dumps(
User.objects.filter(email = email),
content_type =application / json,
status = status.HTTP_200_OK,


现在我想使用,这也是没有渲染输入参数



最后我查看源代码,发现django-rest-swagger需要get_serializer_class来构建身体参数



所以它使用以下代码

  class isEmailTaken(views.APIView):
permission_classes = [ permission.AllowAny,]
serializer_class = IsEmailTakenSerializer

def get_serializer_class(s​​elf):
return self.serializer_class

def post(self,request,* args,** kwargs):
try:
email = request.DATA ['email']
除了KeyError:
return HttpResponse(
'A
status = status.HTTP_400_BAD_REQUEST,

返回HttpResponse(
json.dumps(
User.objects.filter (email = email),
content_type =application / json,
status = status.HTTP_200_OK,


/ pre>

和IsEmailTakenSerializer

  from rest_framework import serializers 
class IsEmailTakenSerializer(serializers.Serializer):
email = serializers.EmailField()


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 API文档不会显示/正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆