0

我正在尝试通过 iOS 应用程序启用用户创建和登录/注销。我已经UserResource成功创建并注册了。我还在login该资源中编写了一个函数:

class UserResource(ModelResource):
class Meta:
    queryset = User.objects.all()
    fields = ['username', 'email']
    serializer = PrettyJSONSerializer()
    authorization = Authorization()


def login(self, request, **kwargs):
    self.method_check(request, allowed=['post'])

    username = request.POST['username']
    password = request.POST['password']

    user = authenticate(username = username, password = password)

    if user:
        if user.is_active:
            login(request, user)
            return self.create_response(request, {'success' : True})
        else:
            return self.create_response(request, {'success' : False, 'reason' : 'disabled'}, HttpForbidden)
    else:
        return self.create_response(request, {'success' : False, 'reason' : 'incorrect'}, HttpUnauthorized)

def logout(self, request, **kwargs):
    pass

当我尝试

 curl -v -H "Content-Type: application/json" -X POST --data '{"username":"username", "password":"password"}' xxx.xxx.xx.xx:8000/api/v1/user/login/?format=json

它返回:

* About to connect() to xxx.xxx.xx.xx port 8000 (#0)
*   Trying xxx.xxx.xx.xx...
* connected
* Connected to xxx.xxx.xx.xx (xxx.xxx.xx.xx) port 8000 (#0)
> POST /api/v1/user/login/?format=json HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5
> Host: xxx.xxx.xx.xx
> Accept: */*
> Content-Type: application/json
> Content-Length: 55
> 
* upload completely sent off: 55 out of 55 bytes
< HTTP/1.1 501 NOT IMPLEMENTED
< Server: gunicorn/17.5
< Date: Wed, 31 Jul 2013 20:59:42 GMT
< Connection: close
< Transfer-Encoding: chunked
< Vary: Accept
< Content-Type: text/html; charset=utf-8
< 
* Closing connection #0
4

1 回答 1

0

事实证明,访问用户名和密码request.POST是不正确的。经过一番挖掘后,我发现这可以解决问题。

    data = self.deserialize(request, request.body, format = request.META.get('CONTENT_TYPE', 'application/json'))

    username = data.get('email', '')
    password = data.get('password', '')
于 2013-08-02T16:28:02.300 回答