PUT请求到dummygopestpie资源不起作用 [英] PUT request to django tastypie resource not working

查看:86
本文介绍了PUT请求到dummygopestpie资源不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试向我的django tastypie资源发出放置请求,以更新用户信息。到目前为止,我可以发出发布请求,但放置不起作用。

i'm trying to do a put request to my django tastypie resource in order to update user info. Up to now, i can make post request but put is not working.

在我的api.py中,我有以下内容:

In my api.py i have this:

class UserResource(ModelResource):
   class Meta:
       queryset = User.objects.all()
       resource_name = 'auth/user'
       fields = ['username', 'email']
       authentication = BasicAuthentication()
       authorization = DjangoAuthorization()
       filtering = {
           "username": ('exact',),
       }

class UserSignUpResource(ModelResource):
   class Meta:
       object_class = User
       resource_name = 'register_user'
       fields = ['username', 'email' , 'password']
       allowed_methods = ['put','post']
       authentication = BasicAuthentication()
       authorization = DjangoAuthorization()
       queryset = User.objects.all()


    def obj_create(self, bundle, request=None, **kwargs):

         bundle = super(UserSignUpResource, self).obj_create(bundle)

然后使用以下命令测试我:

And to test i'me using this:

import urllib2, json

def post():

    def basic_authorization(user, password):
       s = user + ":" + password
       return "Basic " + s.encode("base64").rstrip()

    data = json.dumps({'username':'david_000', 'email':'davidupdat@gmail.com',   'password':'xxx','provider':'twitter','extra_data':{'access_token':'xxx','id':'xxx'}})

    req = urllib2.Request("http://192.168.1.114:8080/api/stats/register_user/",
    headers = {"Authorization": basic_authorization("xxx","xxx"),"Content-Type": "application/json"}, data = data)
    req.get_method = lambda:'PUT'
    f = urllib2.urlopen(req)

post()

我收到此错误:

Traceback (most recent call last):
File "test.py", line 18, in <module>
   post()
File "test.py", line 16, in post
   f = urllib2.urlopen(req)
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
   return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 406, in open
   response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 519, in http_response
   'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 444, in error
   return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
   result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 527, in http_error_default
   raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: BAD REQUEST

更新:

我尝试这样做,但它给了我相同的错误:

i try this, but it gives me the same error:

def obj_update(self, bundle, request=None, **kwargs):

    bundle = super(UserSignUpResource, self).obj_update(bundle)

有什么想法吗?

推荐答案

这是因为您尝试发出放置请求,但放置请求用于更新记录,而不是一个创造新的。 Tastypie将尝试执行obj_update方法,并期望在某些实例上执行。但是您要向register_user / url发出请求,这显然不是对象实例,因此对这个url使用put请求根本没有任何意义。 PUT用于更新而不是创建。

This is because you are trying to issue a put request, but put request goes for update record and not a creating new one. Tastypie will try to execute obj_update method and it expects to be executed on some instance. But you are making request to register_user/ url which is obviously is not an object instance, so it doesnt make a sense at all to use put request for this url. PUT is for updating, not creating.

这篇关于PUT请求到dummygopestpie资源不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆