简体   繁体   English

Django tastypie发布请求获取401

[英]Django tastypie post request getting 401

I am trying to do a POST request using tastypie in django. 我正在尝试使用Django中的tastypie进行POST请求。

My Resource looks like below: 我的资源如下所示:

class TestResource(ModelResource):
    class Meta:
        queryset = Test.objects.all()
        resource_name = 'test'
        serializer = Serializer(formats=['json','xml'])
        always_return_data = True
        detail_allowed_methods = ['get', 'post', 'put', 'delete']

And I am posting data like below: 我发布如下数据:

import sys
import requests


DATA = {'field1':'posting data', 'field2':'123', 'field3':330303,}

def post(data):
    url = 'http://127.0.0.1:8000/api/test/'
    logging = {'verbose':sys.stderr}
    response = requests.post(url,data=data,config=logging)
    print "RESPONSE STATUS", response.status_code
    print "RESPONSE HEADERS", response.headers



if __name__=='__main__':
    post(DATA)

I am always getting 401, any idea? 我总是收到401,知道吗? What I am doing wrong? 我做错了什么?

Thanks in advance. 提前致谢。

You will need to set 您将需要设置

Authorization=Authorization()

The default authorization implmented by tastypie is tastypie实施的默认授权为

ReadOnlyAuthorization()

which results in, you guessed it, readonly results, hence it will cause errors. 您猜到它导致只读结果,因此将导致错误。

http://django-tastypie.readthedocs.org/en/latest/tutorial.html http://django-tastypie.readthedocs.org/en/latest/tutorial.html

Do take note that it is not the best practice to do that however. 请注意,但这不是最佳做法。 There are other methods of Authorizations that can be used. 可以使用其他授权方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM