简体   繁体   English

“详细信息”:“方法 \\”GET\\” 不允许。在 Django 中调用端点

[英]“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 .您需要为getpost提供单独的方法。 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方法也创建了一个实例,那么您只需在getpost方法中调用当前的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.我在我的应用程序上设置了从httphttps的永久重定向,因此当服务器重定向时,向http版本发送POST以某种方式导致GET

TL;DR TL; 博士

HTTP instead of HTTPS HTTP 而不是 HTTPS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 { "detail": "方法 \\"GET\\" 不允许。" } - { "detail": "Method \"GET\" not allowed." } "detail": "方法 \\"GET\\" 不允许。" 在 TokenAuthentication Django 休息框架中 - "detail": "Method \"GET\" not allowed." in TokenAuthentication Django rest framework django“详细信息”:“方法\\”GET\\“不允许。” (一对多关系) - django “detail”: “Method \”GET\“ not allowed.” (one to many relationship) "detail": "方法 \"GET\" 不允许。" Django Rest 框架 - "detail": "Method \"GET\" not allowed." Django Rest Framework “详细信息”:“方法 \”GET\“ 不允许。” Django Rest 框架 - “detail”: “Method \”GET\“ not allowed.” Django Rest Framework DRF:“详细信息”:“方法\\” GET \\“不允许。” - DRF: “detail”: “Method \”GET\“ not allowed.” POST 返回“详细信息”:“不允许使用方法 \"GET\"。” - POST returns "detail": "Method \"GET\" not allowed." "detail": "方法 \\"POST\\" 不允许。" - "detail": "Method \"POST\" not allowed." Restangular删除不工作(是:DJANGO:{“detail”:“方法'删除'不允许。”}) - Restangular remove not working (was: DJANGO: {“detail”: “Method 'DELETE' not allowed.”}) Django REST 框架 - “方法 \”GET\“ 不允许。” - - Django REST framework - “Method \”GET\“ not allowed.” -
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM