简体   繁体   English

"detail": "方法 \\"POST\\" 不允许。"

[英]"detail": "Method \"POST\" not allowed."

here i tried with custom user login in djangorestfulapi but i am getting error that "detail": "Method \\"POST\\" not allowed."在这里,我尝试在 djangorestfulapi 中使用自定义用户登录,但我收到“详细信息”错误:“不允许方法\\“POST\\”。” . . can anybody please explain where i am getting wrong?有人可以解释一下我哪里出错了吗?

class LoginAPIView(APIView):
    def user_login(self,request,format=None):
        # context = RequestContext(request)
        if request.method == 'POST':
            user = ''' SELECT * FROM users '''
            # Gather the username and password provided by the user.

            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)
            print("auth",str(authenticate(username=username, password=password)))

            if user:
                # Is the account active? It could have been disabled.
                if user.is_active:
                    login(request, user)
                    return HttpResponseRedirect('/')
            else:
                return HttpResponse("xxx")
        else:
            # Bad login details were provided. So we can't log the user in.
            print ("Invalid login details: {0}, {1}".format(username, password))
            return HttpResponse("Invalid login details supplied.")

endpoint url:端点网址:

path('api/login/',LoginAPIView.as_view(), name='login'),

In class based views, you should define separate method for separate http methods like this在基于类的视图中,您应该为这样的单独 http 方法定义单独的方法

class LoginAPIView(APIView):
    def post(self,request,format=None):
        user = ''' SELECT * FROM users '''
            # Gather the username and password provided by the user.

        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(username=username, password=password)
        print("auth",str(authenticate(username=username, password=password)))

        if user:
            # Is the account active? It could have been disabled.
            if user.is_active:
            login(request, user)
            return HttpResponseRedirect('/')
        else:
            return HttpResponse("xxx")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 POST 返回“详细信息”:“不允许使用方法 \"GET\"。” - POST returns "detail": "Method \"GET\" not allowed." { "detail": "方法 \\"GET\\" 不允许。" } - { "detail": "Method \"GET\" not allowed." } DRF:“详细信息”:“方法\\” GET \\“不允许。” - DRF: “detail”: “Method \”GET\“ not allowed.” "detail": "方法 \\"GET\\" 不允许。" 在 TokenAuthentication Django 休息框架中 - "detail": "Method \"GET\" not allowed." in TokenAuthentication Django rest framework django“详细信息”:“方法\\”GET\\“不允许。” (一对多关系) - django “detail”: “Method \”GET\“ not allowed.” (one to many relationship) "detail": "方法 \"GET\" 不允许。" Django Rest 框架 - "detail": "Method \"GET\" not allowed." Django Rest Framework Restangular删除不工作(是:DJANGO:{“detail”:“方法'删除'不允许。”}) - Restangular remove not working (was: DJANGO: {“detail”: “Method 'DELETE' not allowed.”}) “详细信息”:“方法 \\”GET\\” 不允许。在 Django 中调用端点 - “detail”: “Method \”GET\" not allowed. on calling endpoint in django “详细信息”:“方法 \”GET\“ 不允许。” Django Rest 框架 - “detail”: “Method \”GET\“ not allowed.” Django Rest Framework 不允许的方法。 Flask接收GET而不是POST - Method Not Allowed. Flask receives GET instead of POST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM