gpt4 book ai didi

django tastypie 401未经授权

转载 作者:行者123 更新时间:2023-12-04 06:03:35 25 4
gpt4 key购买 nike

我无法让这个为我的生活工作。

我在 api.py 中有这个

class catResource(ModelResource):
class Meta:
queryset = categories.objects.all()
resource_name = 'categories'
allowed_methods = ['get', 'post', 'put']
authentication = Authentication()

所以当我尝试时:

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"name":"Test", "parent": "0", "sort": "1","username":"admin","password":"password"}' http://192.168.1.109:8000/api/v1/categories/

我得到:

HTTP/1.0 401 UNAUTHORIZED
Date: Sat, 21 Sep 2013 10:26:00 GMT
Server: WSGIServer/0.1 Python/2.6.5
Content-Type: text/html; charset=utf-8

模型:

class categories(models.Model):
name = models.CharField(max_length=255)
parent = models.ForeignKey('self', blank=True,null=True)
sort = models.IntegerField(default=0)


def __unicode__(self):
if self.parent:
prefix = str(self.parent)
else:
return self.name
return ' > '.join((prefix,self.name))

@classmethod
def re_sort(cls):
cats = sorted([ (x.__unicode__(),x) for x in cls.objects.all() ])
for i in range(len(cats)):
full_name,cat = cats[i]
cat.sort = i
super(categories,cat).save()
def save(self, *args, **kwargs):
super(categories, self).save(*args, **kwargs)
self.re_sort()

class Admin:
pass

最佳答案

正确缩进(如评论中所述),但您还需要更改授权。默认情况下,Tastypie 使用 ReadOnlyAuthorization,这将不允许您发布。

https://django-tastypie.readthedocs.org/en/latest/authorization.html

class catResource(ModelResource):
class Meta:
queryset = categories.objects.all()
resource_name = 'categories'
allowed_methods = ['get', 'post', 'put']
authentication = Authentication()
authorization = Authorization() # THIS IS IMPORTANT

关于django tastypie 401未经授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18970413/

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