Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Taylor Quinn
Taylor Quinn
20,003 Points

500 when posting modelviewset django rest

I am trying to post data using DRF modelviewsets and Axios. I have tried a couple different options so far with the same result, 500. I am able to get data using axios.get but not able to post data. I am new to rest and using ajax so I apologize if it is something obvious.

Axios call

var csrftoken = Cookies.get('csrftoken');
        axios({
            method: 'post',
            url: "/api/schedules/create",
            data: {
                "emp": this.emp.emp,
                'start_time': this.startTime,
                "end_time": this.endTime,
                "date": this.today,
                "location": this.location
            },
            headers : {"X-CSRFToken" : csrftoken }
          })

    },

Serializer

class SchedSerializer(serializers.ModelSerializer):

    class Meta:
        model = Schedule
        fields = (
            'location',
            'emp',
            'date',
            'start_time',
            'end_time'
        )

View

class SchedViewSet(viewsets.ModelViewSet):
    queryset = Schedule.objects.all()
    serializer_class = serializers.SchedSerializer