1

I can do GET requests via the following:

axios.get('/by/f')
        .then(function (data) {
            this.setState({list: data.data});
        }.bind(this))
        .catch(function (error) {
            console.log(error);
        });

However, when I try a PUT request to update my django database, I get a 403 error:

axios({
        method: 'put',
        url: '/by/f',
        data: {
            item: item,
            frequency: frequency
        }
    }).then(function (response) {
        console.log(response);
    });

My view:

class FrequencyList(APIView):
def get(self, request, format=None):
    frequency = Frequency.objects.all()
    serializer = FrequencySerializer(frequency, many=True)
    return Response(serializer.data)

def put(self, request, pk, format=None):
    frequency = self.get_object(pk)
    serializer = FrequencySerializer(frequency, data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

my url pattern:

urlpatterns = [
    path('f', views.FrequencyList.as_view()),
]

my headers:

headers: {
  'Access-Control-Allow-Origin': '*'
},
2
  • Please share the error Commented Feb 26, 2018 at 5:50
  • Your url pattern does not have any integer. Then how will you going to receive pk. The problem is that when you call '/by/f', url will search for another view and there is no put function implemented. Commented Feb 26, 2018 at 6:04

1 Answer 1

1

Your FrequencyList view is fine for getting all frequency list and adding new frequencies. In other words get and post request are fine for FrequencyList view. But you cann't use the same view for detail api(update/delete/getDeatails). For these kid of operations you need the id of the frequency. So for update(put function) create new api/view.

url.py

urlpatterns = [
    path('f', views.FrequencyList.as_view()),
    path('f/(?P<pk>[0-9]+)$', views.FrequencyDetail.as_view()),

views.py

class FrequencyList(APIView):
    def get(self, request, format=None):
        frequency = Frequency.objects.all()
        serializer = FrequencySerializer(frequency, many=True)
        return Response(serializer.data)

class FrequencyDetail(APIView)
    def put(self, request, pk, format=None):
        frequency = Frequency.objects.get(id=pk)
        serializer = FrequencySerializer(frequency, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

and your script should be

axios({
        method: 'put',
        url: '/by/f/' + frequency, # hope frequency is the id of the object that you want to update
        data: {
            item: item,
        }
    }).then(function (response) {
        console.log(response);
    });

And no change in get

axios.get('/by/f')
        .then(function (data) {
            this.setState({list: data.data});
        }.bind(this))
        .catch(function (error) {
            console.log(error);
        });

UPDATE

frequency = Frequency.objects.get(id=pk) can be replace by the following code if you want to handle object not found error

queryset = Frequency.objects.all()
frequency = get_object_or_404(queryset, pk=pk) # from django.shortcuts import get_object_or_404
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, everything is almost working so far. The only thing is I get a 403 Forbidden Error when axios.get gets called. I can do a proper put in the form when to go to localhost:8000/f/1 for example. But the axios.get call doesn't work. Do you know why?
so the put request is working and the get request fails.is that the problem. Then whats the response and request you have made

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.