Django class not counted; POST method not allowed

I participate in the django training process.

I get a 405 error method message. I have a POST defined in my view class as shown 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 the user application is as follows:

url(r'^login/$', LoginView.as_view(), name='login'),

urls.py at the project level:

url(r'^users/', include(users_urls)),

what does my url do

http: // localhost: 8000 / users / login /

I see the url from the log as above.

JS outer corner 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;
    };  

});

+4
source share
1 answer

Perhaps you put http: // localhost: 8000 / api / v1 / users / login instead of http: // localhost: 8000 / api / v1 / users / login / (note the trailing slash)

url(r'^login/$', LoginView.as_view(), name='login'),

to

url(r'^login/?$', LoginView.as_view(), name='login'),
0

Source: https://habr.com/ru/post/1628060/


All Articles