0

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 

Console Output

enter image description here

Taylor
  • 1,223
  • 1
  • 15
  • 30
  • What is the error message you're receiving from Django? – bozdoz Dec 22 '17 at 17:40
  • @bozdoz I added a screenshot of the console output – Taylor Dec 22 '17 at 17:45
  • Right, but there should be some message. Are there error logs you can check? Or is DEBUG mode on? – bozdoz Dec 22 '17 at 17:46
  • @bozdoz Debug mode is on. The page does not show any logging output, I am assuming this is because it is an ajax call – Taylor Dec 22 '17 at 17:54
  • 1
    @bozdoz I realized I was missing a trailing slash in the url in the ajax call but now I get a 405 (Method Not Allowed). Any thoughts on why that might come up? – Taylor Dec 22 '17 at 17:57
  • 1
    You should be posting the error from your Django console, not the browser's dev tools. – Daniel Roseman Dec 22 '17 at 18:52
  • Try this: https://stackoverflow.com/questions/36011333/405-method-post-is-not-allowed-in-django-rest-framework – bozdoz Dec 22 '17 at 20:38

0 Answers0