получение 403 запрещенной ошибки при попытке отправить запрос на django rest framework через ax ios react - PullRequest
0 голосов
/ 10 июля 2020

Я новичок в Django и React. Я пытаюсь отправить запрос на django через ax ios, но получаю 403 запрещенную ошибку. Пытаюсь отредактировать профиль с помощью запроса на размещение. Я использую аутентификацию сеанса. В запросе присутствует sessionid и токен csrftoken.

views.py

class TeacherUpdateViewSet(viewsets.ModelViewSet):

queryset = CustomUser.objects.filter(is_teacher=True)
serializer_class = TeacherProfileUpdateSerializer
lookup_field = 'username'

@method_decorator(ensure_csrf_cookie)
def dispatch(self, *args, **kwargs):
    return super(TeacherUpdateViewSet, self).dispatch(*args, **kwargs)

serializers.py

class TeacherSerializer(serializers.ModelSerializer):

class Meta:
    model = Teacher
    fields = [ 'full_name', 'slug', 'introduction_video', 'poster', 'profile_picture', 'introduction', 'qualification', 'phone_number', 'subjects', 'state']
    depth = 1

class TeacherProfileUpdateSerializer(serializers.HyperlinkedModelSerializer):

teacher_profile = TeacherSerializer()

class Meta:
    model = CustomUser
    fields = ['username', 'teacher_profile']

def update(self, instance, validated_data):
    print("entered update")
    profile_data = validated_data.pop('teacher_profile')
    teacher_profile = instance.teacher_profile

    teacher_profile.phone_number = profile_data.get('phone_number', teacher_profile.phone_number)
    teacher_profile.state = profile_data.get('state', teacher_profile.state)
    teacher_profile.introduction = profile_data.get('introduction', teacher_profile.introduction)
    teacher_profile.qualification = profile_data.get('qualification', teacher_profile.qualification)
    teacher_profile.profile_picture = profile_data.get('profile_picture', teacher_profile.profile_picture)
    teacher_profile.introduction_video = profile_data.get('introduction_video', teacher_profile.introduction_video)
    teacher_profile.save()

    return instance

request

Axios({ method: 'put', 
                
                url: `http://127.0.0.1:8000/update/${username}/`,
                headers: {
                    'Content-Type' : 'application/json',
              
                    }, 

                data:JSON.stringify(data),
                withCredentials: true,
         })
        .then(response => {
            console.log(response)
            
            this.setState({
              formSubmitted : true,
              loading : false,
            })
            
        })
        .catch(error => {
            this.setState({
              error : true,
              loading : false,
            })
            console.log(error)      
        })
}
});
...