gpt4 book ai didi

python - PUT 方法在 django-tastypie 中不起作用?

转载 作者:行者123 更新时间:2023-12-01 05:37:20 24 4
gpt4 key购买 nike

我是 django-tastypie 的新手。这是我的 api.py 代码,

from tastypie.resources import ModelResource
from .models import ListModel


class ListModelResource(ModelResource):

def determine_format(self, request):
return 'application/json'

class Meta:
allowed_methods = ['get','put']
queryset = ListModel.objects.all()

这里我使用 CURL 进行 GET:

curl http://127.0.0.1:8000/api/v1/listmodel/1/

OUT: {"completed": false, "id": 1, "resource_uri": "/api/v1/listmodel/1/", "title": "This is test"}

这里我使用 CURL 进行 PUT:

 curl --dump-header - -H "Content-Type: application/json" '{"completed": false, "id": 1, "resource_uri": "/api/v1/listmodel/1/", "title": "This is test"}' http://127.0.0.1:8000/api/v1/listmodel/1/
HTTP/1.0 401 UNAUTHORIZED
Date: Wed, 04 Sep 2013 08:12:53 GMT
Server: WSGIServer/0.1 Python/2.7.2+
Content-Type: text/html; charset=utf-8

为什么我收到 401 ?

最佳答案

根据tastypie tutorial :

... if you try sending a POST/PUT/DELETE to the resource, you find yourself getting “401 Unauthorized” errors. For safety, Tastypie ships with the authorization class (“what are you allowed to do”) set to ReadOnlyAuthorization. This makes it safe to expose on the web, but prevents us from doing POST/PUT/DELETE. ..

您可以使用tastypie.authorization.Authorization启用它:

from tastypie.authorization import Authorization
from tastypie.resources import ModelResource
from .models import ListModel

class ListModelResource(ModelResource):
def determine_format(self, request):
return 'application/json'

class Meta:
allowed_methods = ['get','put']
queryset = ListModel.objects.all()
authorization= Authorization() # <---

Warning

This is now great for testing in development but VERY INSECURE. You should never put a Resource like this out on the internet. Please spend some time looking at the authentication/authorization classes available in Tastypie.

关于python - PUT 方法在 django-tastypie 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18609012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com