2016-11-16 76 views
1

我試圖讓一個Tastypie登錄系統工作,但是當我發送一個JSON請求與用戶數據時,服務器響應Http 500.我已經查看了我的所有代碼並嘗試調試的問題,但沒有我試着工作。Tastypie登錄返回HttpResonse 500

我環顧四周,找不到任何東西來幫助我。

有誰知道我該如何去解決這個問題,或者如果有什麼我應該做的,我不是。

謝謝。

api.py

class UserResource(ModelResource): 
    raw_password = fields.CharField(attribute=None, readonly=True, null=True, blank=True) 

    class Meta: 
     queryset = User.objects.all() 
     fields = ['first_name', 'last_name', 'email'] 
     allowed_methods = ['get', 'post' ] 
     resource_name = 'user' 

    def prepend_urls(self): 
     return [ 
      url(r"^(?P<resource_name>%s)/login%s$" % 
       (self._meta.resource_name, trailing_slash()), 
       self.wrap_view("login"), name="api_login"), 
      url(r'^(?P<resource_name>%s)/logout%s$' % 
       (self._meta.resource_name, trailing_slash()), 
       self.wrap_view("logout"), name="api_logout"), 
     ] 

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

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

     username = data.get('username', '') 
     password = data.get('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', 
       }, HttpUnauthorized) 
     else: 
      return self.create_response(request, { 
       'success': False, 
       'reason': 'incorrect', 
      }, HttpUnauthorized) 

    def logout(self, request, **kwargs): 
     self.method_check(request, allowed=['get']) 
     if request.user and request.user.authenticated(): 
      logout(request) 
      return self.request.create_response(request, {'success': True}) 
     else: 
      return self.request.create_response(request, {'success': False}, HttpUnauthorized) 

request.py

import json 
import requests 

data = {"username" : "test", "password" : "password"} 
headers = {"content-type": "application/json"} 
url = "http://localhost:8000/api/v1/user/login/" 

response = requests.post(url, data=json.dumps(data), headers=headers) 

print response 

回答

0

問題是與request.raw_post_data它的棄用,應改爲request.body希望這可以幫助其他人有問題。

+0

這將是更好,如果接受你自己的答案 –

+0

我會,但它不會讓我2天。一旦我能夠,我會的。 – geolaw

1

試試這個,它適用於我!

api.py

class UserResource(ModelResource): 
    raw_password = fields.CharField(attribute=None, readonly=True, null=True, blank=True) 

    class Meta: 
     queryset = User.objects.all() 
     allowed_methods = ['get', 'post' ] 
     resource_name = 'user' 
     authentication = Authentication() 
     authorization = Authorization() 
     serializer = Serializer(formats=['json']) 


    def prepend_urls(self): 
     return [ 
      url(r"^(?P<resource_name>%s)/login%s$" % 
       (self._meta.resource_name, trailing_slash()), 
       self.wrap_view("login"), name="api_login"), 
      url(r'^(?P<resource_name>%s)/logout%s$' % 
       (self._meta.resource_name, trailing_slash()), 
       self.wrap_view("logout"), name="api_logout"), 
     ] 

    def login(self, request, **kwargs): 
     self.method_check(request, allowed=['post']) 
     data = self.deserialize(request, request.body, format=request.META.get('CONTENT_TYPE', 'application/json')) 
     username = data.get('username', '') 
     password = data.get('password', '') 


     user = authenticate(username=username, password=password) 
     print user 
     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', 
       }, HttpUnauthorized) 
     else: 
      return self.create_response(request, { 
       'success': False, 
       'reason': 'incorrect', 
      }, HttpUnauthorized) 


    def logout(self, request, **kwargs): 
     """ 
     Attempt to log a user out, and return success status. 
     """ 
     self.method_check(request, allowed=['get']) 

     # Run tastypie's BasicAuthentication 
     self.is_authenticated(request) 

     if request.user and request.user.is_authenticated(): 
      logout(request) 
      return self.create_response(request, { 'success': True }) 
     else: 
      return self.create_response(request, { 'success': False, 'error_message': 'You are not authenticated, %s' % request.user.is_authenticated() }) 

request.py

import json 
import requests 

data = {"username" : "test", "password" : "password"} 
headers = {"content-type": "application/json"} 
url = "http://localhost:8000/api/v1/user/login/" 

response = requests.post(url, data=json.dumps(data), headers=headers) 
print response 

在終端

In [2]: import requests 

In [3]: import json 

In [4]: data = {"username" : "test", "password" : "password"} 

In [5]: headers = {"content-type": "application/json"} 

In [6]: url= "http://localhost:8000/api/v1/user/login/" 

In [7]: response = requests.post(url, data=json.dumps(data), headers=headers) 

In [8]: response 
Out[8]: <Response [200]>