[英]“detail”: “Method \”GET\" not allowed. on calling endpoint in django
I'm using django.rest_framework.我正在使用 django.rest_framework。 I have a get_or_create method for a particular view,我有一个特定视图的 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)
This is the location Model,这是位置模型,
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')
This is my url,这是我的网址,
url(r'^location/$', LocationView.as_view(), name='location'),
When I call this endpoint in the following way, http://127.0.0.1:8000/api/v1/bouncer/location/?country=USA&&city=Sunnyvale&&latitude=122.0363&&longitude=37.3688当我通过以下方式调用此端点时, http://127.0.0.1:8000/api/v1/bouncer/location/?country=USA&&city=Sunnyvale&&latitude=122.0363&&longitude=37.3688
This is what I get,这就是我得到的
{
"detail": "Method \"GET\" not allowed."
}
What am I missing here.我在这里错过了什么。
The Method not allowed
error is because, it searches for a get()
method inside your API class, and it couldn't find a one. Method not allowed
错误是因为它在您的 API 类中搜索get()
方法,但找不到。
The general format of the API class is as below 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")
If you want to invoke the get_or_create()
method at some point, you can do it as any other methods,如果您想在某个时候调用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")
You need to provide separate methods for get
and post
.您需要为get
和post
提供单独的方法。 If your get
method also creates an instance, then you can just call your current get_or_create
inside both get
and post
methods.如果您的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)
For my situation, I was sending a request properly as my view would expect, a POST
.对于我的情况,我按照我的观点正确发送了一个请求,一个POST
。 But the issue was on http/s
.但问题出在http/s
。 I had set a permanent redirect, from http
to https
on my app, and so sending a POST
to the http
version resulted in a GET
somehow when the server was redirecting.我在我的应用程序上设置了从http
到https
的永久重定向,因此当服务器重定向时,向http
版本发送POST
以某种方式导致GET
。
TL;DR TL; 博士
HTTP instead of HTTPS HTTP 而不是 HTTPS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.