"detail": "Method \"GET\" not allowed. 在 django 中调用端点
Posted
技术标签:
【中文标题】"detail": "Method \\"GET\\" not allowed. 在 django 中调用端点【英文标题】:"detail": "Method \"GET\" not allowed. on calling endpoint in django"detail": "Method \"GET\" not allowed. 在 django 中调用端点 【发布时间】:2018-08-22 10:49:11 【问题描述】:我正在使用 django.rest_framework。我有一个特定视图的 get_or_create 方法,
class LocationView(views.APIView):
def get_or_create(self, request):
try:
location = Location.objects.get(country=request.data.get("country"), city=request.data.get("city"))
Response(location, status=status.HTTP_200_OK)
except Location.DoesNotExist:
serializer = LocationSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
这是位置模型,
class Location(models.Model):
country = models.CharField(max_length=255)
city = models.CharField(max_length=255, unique=True)
latitude = models.CharField(max_length=255)
longitude = models.CharField(max_length=255)
class Meta:
unique_together = ('country', 'city')
这是我的网址,
url(r'^location/$', LocationView.as_view(), name='location'),
当我通过以下方式调用此端点时, http://127.0.0.1:8000/api/v1/bouncer/location/?country=USA&&city=Sunnyvale&&latitude=122.0363&&longitude=37.3688
这就是我得到的,
"detail": "Method \"GET\" not allowed."
我在这里错过了什么。
【问题讨论】:
将get_or_create()
更改为get()
但我需要get_or_create,它有不同的用途。
相关:***.com/questions/49181262/…
由于 url 中的双斜杠(由 postman 环境变量引起),我遇到了这个错误。例如api.example.com/v1//accounts
也可能导致此错误。通过删除额外的斜线轻松修复
【参考方案1】:
对于我的情况,我按照我的预期正确发送了一个请求,POST
。但问题出在http/s
。我在我的应用程序上设置了一个永久重定向,从http
到https
,因此当服务器重定向时,将POST
发送到http
版本会导致GET
。
TL;DR
HTTP 代替 HTTPS
【讨论】:
【参考方案2】:Method not allowed
错误是因为它在您的 API 类中搜索 get()
方法,但找不到。
API类的一般格式如下
class LocationView(views.APIView):
def get(self, request):
#do something with 'GET' method
return Response("some data")
def post(self, request):
#do something with 'POST' method
return Response("some data")
如果您想在某个时候调用 get_or_create()
方法,您可以像其他任何方法一样执行此操作,
class LocationView(views.APIView):
def get_or_create(self, request):
# do some "get or create" stuff
return "some data"
def get(self, request):
if condition:
self.get_or_create(request)
# do some stuff
return Response(" some special data related to get or create")
return Response("some data")
【讨论】:
【参考方案3】:您需要为get
和post
提供单独的方法。如果您的get
方法也创建了一个实例,那么您可以在get
和post
方法中调用您当前的get_or_create
。
class LocationView(views.APIView):
def get_or_create(self, request):
# your current definition here
def get(self, request):
return self.get_or_create(request)
def post(self, request):
return self.get_or_create(request)
【讨论】:
您能否从我现有的代码中提供一个小代码示例。这将有助于理解。 我还建议你看看默认的Manager.get_or_create
Django 提供的,所以你不必在try except
docs.djangoproject.com/en/2.0/ref/models/querysets/… 中自己做逻辑以上是关于"detail": "Method \"GET\" not allowed. 在 django 中调用端点的主要内容,如果未能解决你的问题,请参考以下文章
"detail": "Method \"GET\" not allowed. 在 django 中调用端点
"errors":"errors":["detail":"您无权执行此操作。","code":&qu
模型 \"Trains\ 的路径 \"setno\" 处的值 \" details: '233' \" 转换为字符串失败
为啥响应总是 "detail":"Unsupported media type \"text/plain\" in request." 在