I am in the process of learning django.
I am getting method post not allowed 405 error. I have POST defined in my view class as below.
class LoginView(views.APIView):
def get_permissions(self):
if self.request.method in permissions.SAFE_METHODS:
return (permissions.AllowAny(),)
if self.request.method == 'POST':
return (permissions.AllowAny(),)
def post(self, request, format=None):
data = json.loads(request.body)
email = data.get('email', None)
password = data.get('password', None)
account = authenticate(email=email, password=password)
if account is not None:
if account.is_active:
login(request, account)
serialized = HUserAuthSerializer(account)
return Response(serialized.data)
else:
return Response({
'status': 'Unauthorized',
'message': 'This account has been disabled.'
}, status=status.HTTP_401_UNAUTHORIZED)
else:
return Response({
'status': 'Unauthorized',
'message': 'Username/password combination invalid.'
}, status=status.HTTP_401_UNAUTHORIZED)
urls.py in users app has following:
url(r'^login/$', LoginView.as_view(), name='login'),
urls.py at project level has following:
url(r'^users/', include(users_urls)),
that makes my URL
http://localhost:8000/users/login/
I can see the URL from in the log as above.
frontend angularJS code is as follows:
appData.service("signInService", function($http, $q) {
this.signIn = function (signin) {
var url = "http://localhost:8000/users/login/";
console.log(url);
var defer = $q.defer();
$http.post(url, {
email: signin.email,
password: signin.password},
{callback:"JSON_CALLBACK", _dont_enforce_csrf_checks:"True"}, {post:{method: "JSONP"}})
.success(function(response){
defer.resolve(response);
})
.error(function(response){
defer.reject(response);
})
return defer.promise;
};
});
Are you possibly posting to http://localhost:8000/api/v1/users/login instead of http://localhost:8000/api/v1/users/login/ (notice the trailing slash)
Try modifying
url(r'^login/$', LoginView.as_view(), name='login'),
to
url(r'^login/?$', LoginView.as_view(), name='login'),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With