在Django 4 Rest Framework中,要使用POST方法,需要先进行身份验证。可以添加以下代码进行身份验证并允许使用POST方法:
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import authentication_classes, permission_classes
# 添加身份验证和权限验证
@authentication_classes([])
@permission_classes([IsAuthenticated])
class YourView(APIView):
def post(self, request):
# 处理POST请求的代码
上述代码将取消身份验证和权限验证,允许未经身份验证的用户使用POST方法。如果需要进行身份验证和权限验证,请根据实际需求进行配置。