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. 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的文档。我安装了软件包并在三引号之间插入了您在上面看到的评论。当我查看django-rest-swagger为此API生成的文档时,我会看到下面的图片。
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.
但是,当我点击试试吧!按钮,我收到如下所示的错误。值得注意的是,它从未让我有机会输入它应该通过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?
为什么Django-Swagger-Package没有创建允许我通过POST正确地使用“email”参数的文档?我该如何工作?
2 个解决方案
#1
12
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()
#2
1
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表单字段。
#1
12
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()
#2
1
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表单字段。