【问题标题】:DRF: "detail": "Method \"GET\" not allowed."DRF:“详细信息”:“方法 \"GET\" 不允许。”
【发布时间】:2018-03-02 14:51:54
【问题描述】:

我正在尝试使用 DRF 创建 registersign in API(TokenAuthentication)。

这是我的观点.py

from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import AllowAny

from .serializers import AccountSerializer
from .models import Account

class AuthRegister(APIView):
    """
    Register a new user.
    """
    serializer_class = AccountSerializer
    permission_classes = (AllowAny,)

    def get(self, request, format=None):
        allquery = Account.objects.all()
        serializer = AccountSerializer(allquery, many=True)
        return Response(serializer.data)

    def post(self, request, format=None):
        serializer = self.serializer_class(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data,
                    status=status.HTTP_201_CREATED)
        return Response(serializer.errors,
        status=status.HTTP_400_BAD_REQUEST)

class AuthLogin(APIView):
    ''' Manual implementation of login method '''

    def get(self, request, format=None):
        allquery = Account.objects.all()
        serializer = AccountSerializer(allquery, many=True)
        return Response(serializer.data)

    def post(self, request, format=None):
        data = request.data
        email = data.get('email', None)
        password = data.get('password', None)

        account = authenticate(email=email, password=password)
        # Generate token and add it to the response object
        if account is not None:
            login(request, account)
            return Response({
                'status': 'Successful',
                'message': 'You have successfully been logged into your account.'
            }, status=status.HTTP_200_OK)

        return Response({
            'status': 'Unauthorized',
            'message': 'Username/password combination invalid.'
        }, status=status.HTTP_401_UNAUTHORIZED)

这是 urls.py

from django.conf.urls import url
from .views import AuthRegister
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token, verify_jwt_token

urlpatterns = [
    url(r'^login/', obtain_jwt_token),
    url(r'^token-refresh/', refresh_jwt_token),
    url(r'^token-verify/', verify_jwt_token),
    url(r'^register/$', AuthRegister.as_view()),
]

当我运行服务器时,我收到以下错误。

提交详细信息后,用户被添加到数据库中,但当我登录时发生错误。

"Unable to log in with provided credentials."

无法找出错误。请任何人帮助!

【问题讨论】:

    标签: python django django-rest-framework


    【解决方案1】:

    您尚未在 AuthRegister 视图中实现 get 方法。 DRF 不知道如何响应该视图的 GET 请求,因此它假定您不希望允许此类请求。

    您应该实现一个 get 方法,该方法实现对 GET 请求要采取的操作(您的服务器响应)。

    【讨论】:

    • 我应该在get()返回什么?
    • AuthLogin 怎么样,我在其中实现了相同的get(),它会给出错误
    • @YatharthaJoshi 我在AuthLogin 中也没有看到任何def get(...)
    • 本地修改了,这里也修改
    猜你喜欢
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2017-04-26
    • 1970-01-01
    相关资源
    最近更新 更多