[英]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: 我已经构建了一个Django API,当通过POST给出一个电子邮件地址时,将使用一个布尔值来响应,表明我的数据库中已存在该电子邮件地址的天气:
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. 现在我想使用django-rest-swagger包自动生成此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.
当我查看django-rest-swagger为此API生成的文档时,我会看到下面的图片。
However, when I click the Try it out!
但是,当我点击
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.
值得注意的是,它从未让我有机会输入它应该通过POST发送的电子邮件参数。
Why doesn't the Django-Swagger-Package create docs that allow me to properly the argument "email" via POST? 为什么Django-Swagger-Package不创建允许我通过POST正确地使用“email”参数的文档? 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 我使用由django-rest-swagger制作的
cigar_example
测试了这个,在那个例子中,他们编写了一个自定义视图 ,它也没有渲染输入参数
Lastly i look into source code and found that django-rest-swagger needs get_serializer_class
to build body parameters 最后我查看源代码,发现django-rest-swagger需要
get_serializer_class
来构建body参数
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
: 和
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. django-rest-swagger尝试发送没有数据的POST请求。
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. 下一步是查看django-rest-swagger文档以找出要做的事情,它会在“试一试”按钮上方呈现一个html表单字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.