我試圖讓一個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
這將是更好,如果接受你自己的答案 –
我會,但它不會讓我2天。一旦我能夠,我會的。 – geolaw