1. 程式人生 > >django CSRF token missing or incorrect

django CSRF token missing or incorrect

一開始的解決方法:

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
    def post(self, request):
        """
        簡訊驗證碼校驗
        """
        mobile = request.query_params.get('mobile')
        if not re.match(r'^1[345789]\d{9}$', mobile):
            return Response({'message': '手機號格式錯誤'})
        sms_code = request.query_params.get('sms_code')

去掉csrf還是不行, 我用的django + drf  原來是drf預設會去執行驗證csrf的

所以應該在把加上這一串程式碼

class SMSCodeView(GenericAPIView):
    """
    簡訊驗證碼
    """
    serializer_class = serializers.SMSCodeSerializer
    authentication_classes = []