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': '*'
},
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
views.py
and your script should be
And no change in get
UPDATE
frequency = Frequency.objects.get(id=pk)
can be replace by the following code if you want to handle object not found error