Reputation: 161
I am new to Django and python. I have a website that already in production using docker. And the API URL is on: http://gmlews.com/api/data. When I want to test the API using postman, the GET method working fine, but for the POST method is returned response 200 OK not 201 created. Because of that, my data can't be saved in the API.
Here is my code for the API :
restapi/serializers.py:
from .models import Data,Node
from rest_framework import serializers
class DataSerializer(serializers.ModelSerializer):
class Meta:
model = Data
fields = '__all__'
class NodeSerializer(serializers.ModelSerializer):
class Meta :
model = Node
fields = '__all__'
restapi/views.py:
import json
from django.views.generic import View
from django.shortcuts import render
from rest_framework import routers, serializers, viewsets
from rest_framework.response import Response
from restapi.serializers import DataSerializer, NodeSerializer
from restapi.models import Data, Node
from django_filters.rest_framework import DjangoFilterBackend
from django.http import HttpResponse
from rest_framework.views import APIView
# Create your views here.
class DataViewSet(viewsets.ModelViewSet):
queryset = Data.objects.all()
serializer_class = DataSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = ['node_id']
class MapViewSet(viewsets.ModelViewSet):
queryset = Data.objects.filter(node_id=1).order_by('-id')[:1]
serializer_class = DataSerializer
class NodeViewSet(viewsets.ModelViewSet):
queryset = Node.objects.all()
serializer_class = NodeSerializer
urls.py:
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import include, path
from restapi import views
""" for routers api root"""
from restapi.models import Data, Node
from rest_framework import routers
from restapi.views import DataViewSet, NodeViewSet, MapViewSet
router = routers.DefaultRouter()
router.register(r'data', DataViewSet, 'data')
router.register(r'node', NodeViewSet, 'node')
router.register(r'map', MapViewSet, 'map')
urlpatterns = [
path('admin/', admin.site.urls),
path(r'api/', include(router.urls)),
path(r'', include('rest_framework.urls', namespace='rest_framework')),
]
Can someone help me with this issue? I really don't have an idea why the POST method received status 200. I want it to received status 201 so my data can be saved. Thank you
Upvotes: 0
Views: 1958
Reputation: 3280
Acessed your site and did a post. It returned a 201. Follow is the evidence:
If you access you data, the item with id 15 was created by me, and the current response for that POST
was a 201.
Upvotes: 0