I'm trying to get a Tastypie login system to work, however when I send a json request with the user data, the server responds with Http 500. I have looked through all of my code and tried to debug the issue but nothing I try is working.
I've looked around and can't find anything to help me.
Does anyone know how I can go about fixing this issue or if there is something I should be doing that I am not.
Thanks.
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)
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
Issue was with request.raw_post_data
it's deprecated and should be changed to request.body
hope this helps anyone else having issues.