详细信息”:“不允许使用方法“GET”,|Django休息框架

nue99wik  于 2023-01-21  发布在  Go
关注(0)|答案(4)|浏览(224)

我正在学习用户登录和注册课程,遇到此错误,我不知道如何修复detail": "Method \"GET\" not allowed.
urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('users/login/', views.MyTokenObtainPairView.as_view(), name='token_obtain_pair'),
    
    path('users/register/', views.registerUser, name='register'),

    path('users/profile/', views.getUserProfile, name="users-profile"),
    path('users/', views.getUsers, name="users"),
]

views.py

@api_view(['POST'])
def registerUser(request):
    data = request.data
    user = User.objects.create(
        first_name = data['name'],
        username = data['email'],
        email = data['email'],
        password = make_password(data['password'])
    )

    serializer = UserSerializerWithToken(user, many=False)

    return Response(serializer.data)

serializers.py

class UserSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField(read_only=True)
    _id = serializers.SerializerMethodField(read_only=True)
    isAdmin = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = User
        fields = ['id', '_id', 'username', 'email', "name", "isAdmin"]

    def get__id(self, obj):
        return obj.id

    def get_isAdmin(self, obj):
        return obj.is_staff
    
    def get_name(self, obj):
        name = obj.first_name
        if name == '':
            name = obj.email

        return name

class UserSerializerWithToken(UserSerializer):
    token = serializers.SerializerMethodField(read_only=True)
    
    class Meta:
        model = User
        fields = ['id', '_id', 'username', 'email', "name", "isAdmin", 'token']

    def get_token(self, obj):
        token = RefreshToken.for_user(obj)
        return str(token.access_token)

我不确定这是否是问题所在,但我看到有人说,由于在www.example.com中views.py我的视图是@api_view(['POST']),因此无法在www.example.com中以通常urls.py的方式调用它。

vsdwdz23

vsdwdz231#

由于您的API设置为只接受POST方法,因此您不能简单地将URL输入浏览器。api_view(['POST'])
尝试使用POST方法在postman中运行API
https://www.django-rest-framework.org/api-guide/views/#api_view
或添加以下内容

@api_view(['POST', 'GET'])
def registerUser(request):
    if request.method == 'GET':
        return Response({'message': 'In get'})
    data = request.data
    user = User.objects.create(
        first_name = data['name'],
        username = data['email'],
        email = data['email'],
        password = make_password(data['password'])
    )

    serializer = UserSerializerWithToken(user, many=False)

    return Response(serializer.data)
3htmauhk

3htmauhk2#

如错误所示,您的寄存器API应该是POST,而不是GET
如果您使用GET方法调用您的register API,则会出现上述错误。
您可以简单地使用curl命令进行测试(假设在端口8000上运行服务器)。

curl -X GET http://localhost:8000/users/register/

这将导致“方法不允许”错误

curl -d '{"name":"value1", "email":"value2", "password": "value3"}' \
  -X POST http://localhost:8000/users/register/

这会很好的

dauxcl2d

dauxcl2d3#

当我们通过浏览器调用时,默认方法是GET。要通过提供适当的方法和数据来测试这些端点,请使用如下的rest API客户端

  1. POSTMAN
  2. Thunder client if your using VS CODE
  3. curl way @fullswing answer, if you want minimal installation
baubqpgj

baubqpgj4#

在你的API_view中你没有使用GET方法,这就是它给你错误的原因。请使用GET方法,它应该工作。检查下面的参考

@api_view(['GET','POST'])
def registerUser(request):
     data = request.data
     user = User.objects.create(
     first_name = data['name'],
     username = data['email'],
     email = data['email'],
     password = make_password(data['password'])
 )

相关问题