"Method \"GET\"不允许。"姜戈

"Method \"GET\" not allowed." Django

我的views.py文件:

from rest_framework.views import APIView
from rest_framework.response import Response
from .serializers import UserSerializer


class TestView(APIView):
    def get(self, request, format=None):
        print("API called")
        return Response("You did it!", status=200)


class UserView(APIView):
    def post(self, request, format=None):
        print("User created")

        user_data = request.data
        print(request.data)
        user_serializer = UserSerializer(data=user_data)

        if user_serializer.is_valid(raise_exception=False):
            user_serializer.save()
            return Response({'user': user_serializer.data}, status=200)

        return Response({'msg': "error: no user created"}, status=400)

# convert user token to user data


def get(self, request, format=None):

    if request.user.is_authenticated == False or request.user.is_active == False:
        return Response('Invalid credentials', status=403)

    user = UserSerializer(request.user)
    print(user.data)

    return Response("Testing", status=200)

我的serializers.py文件:

from rest_framework import serializers
from django.contrib.auth.models import User
from rest_framework.validators import UniqueValidator
from rest_framework.settings import api_settings


class UserSerializer(serializers.ModelSerializer):

    token = serializers.SerializerMethodField()

    email = serializers.EmailField(
        required=True,
        validators=[UniqueValidator(queryset=User.objects.all())]
    )

    username = serializers.CharField(
        required=True,
        max_length=32,
        validators=[UniqueValidator(queryset=User.objects.all())]
    )

    first_name = serializers.CharField(
        required=True,
        max_length=32
    )

    last_name = serializers.CharField(
        required=True,
        max_length=32
    )

    password = serializers.CharField(
        required=True,
        min_length=8,
        write_only=True
    )

    def create(self, validated_data):
        password = validated_data.pop(password, None)
        instance = self.Meta.model(**validated_data)
        if password is not None:
            instance.set_password(password)
        instance.save()
        return instance

    def get_token(self, obj):
        jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
        jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
        payload = jwt_payload_handler(obj)
        token = jwt_encode_handler(payload)
        return token

    class Meta:
        model = User
        fields = (
            'token',
            'username',
            'password',
            'first_name',
            'last_name',
            'email',
            'id'
        )

我的urls.py文件:

from django.urls import path
from .views import TestView, UserView
from rest_framework_jwt.views import refresh_jwt_token, verify_jwt_token

urlpatterns = [
    path('test', TestView.as_view()),
    path('create-user/', UserView.as_view()),
    path('get-user', UserView.as_view()),
]

我创建了一个用户,他们成功地存储了我的 SQLite 数据库。现在,我想使用他们的令牌获取用户。所以我在 views.py 中创建了一个 GET 方法,并在 urls.py 文件中添加了一个 get-user URL。

邮递员请求:

如您所见,我在“授权”下将 JWT {token} 作为 header 发送。但是,我得到不允许使用 GET 方法。我对出了什么问题感到困惑。

当我发送请求时我的 CMD 上出现的内容:

不确定是否是拼写错误,但是代码块中的 UserView class 之外有 get 方法。

还有一件事,不需要为同一个 APIView 使用两个不同的端点,只需向 create-user 端点发送一个获取请求,或者更好的是只使用 ModelViewSet,因为您已经在使用一个 ModelSerializer。