I'm currently facing an issue where I cannot establish a connection to the Django server. Upon checking the browser console, I noticed the following error message:
[![console error][1]][1]
My setup involves using Tastypie along with a UserResource class:
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
fields = ['first_name', 'last_name', 'email']
allowed_methods = ['get', 'post']
resource_name = 'user'
def override_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',
}, HttpForbidden )
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.is_authenticated():
logout(request)
return self.create_response(request, { 'success': True })
else:
return self.create_response(request, { 'success': False }, HttpUnauthorized)
Additionally, I've implemented a loginController in my client-side code to send a POST request with a username and password:
module.controller('LoginController', ['$scope','$http',
function($scope, $http) {
$http({
method: 'POST',
data: {'username' : 'test', 'password' : 'test123' },
url: 'http://localhost:8000/api/v1/user/login/'
}).then(function successCallback(response) {
console.log("OK Response");
console.log(response.data);
$scope.orders = response.data.objects;
}, function errorCallback(response) {
console.log("NO Response");
});
}]);