我是django restframework的新手。 GET
请求工作正常,在客户端检索了数据库中的数据,但是在执行发布请求时,没有数据添加到数据库中。我想从Django API的react axios检索数据并将其存储在数据库中。它没有显示任何错误,但是也没有在数据库中添加json数据。
这是views.py
class formAPI(APIView):
def get(self,request):
if request.method=='GET':
formData=Form.objects.all()
serializer=FormSerializer(formData,many=True)
return Response(serializer.data)
def post(self,request):
if request.method == 'POST':
data_Serializer = FormSerializer(data=request.data)
if data_Serializer.is_valid():
data_Serializer.save()
return Response(data_Serializer.data,status=status.HTTP_201_CREATED)
return Response({'key': 'value'})
这是models.py
from django.contrib.gis.db import models
from phonenumber_field.modelfields import PhoneNumberField
# Create your models here.
class Form(models.Model):
Province=models.CharField(max_length=100,null=False)
District=models.CharField(max_length=100,null=False)
PalikaType=models.CharField(max_length=100,null=False)
PalikaName=models.CharField(max_length=100,null=False)
Ward_No=models.CharField(max_length=100,null=False)
Ward_Office_Address=models.CharField(max_length=100,null=False)
Ward_Contact_No=PhoneNumberField()
X_Cords=models.FloatField(null=False)
Y_Cords=models.FloatField(null=False)
Chairperson_Name=models.CharField(max_length=100,null=False)
Chaiperson_Contact_No=PhoneNumberField()
Secretary_Name=models.CharField(max_length=100,null=False)
Secretary_Contact_No=PhoneNumberField()
Area=models.FloatField(max_length=100,null=False)
Total_Households=models.IntegerField(null=False)
Total_Population=models.IntegerField(null=False)
Total_Male_Population=models.IntegerField(null=False)
Total_Female_Population=models.IntegerField(null=False)
Website= models.URLField(max_length=200)
Email=models.EmailField(max_length=254)
location= models.PointField(srid=4326,null=False)
def __str__(self):
return 'PalikaName: %s' % self.name
这是要存储的数据
{ Province: "province 1",
District: "bhaktapur",
PalikaType: "gaupalika",
PalikaName: "madhyapur gaupalika",
Ward_No: "5",
Ward_Office_Address: "balkumari",
Ward_Contact_No: "+9779818523950",
X_Cords: 85.385252,
Y_Cords: 27.675952,
Chairperson_Name: "kumar sanu",
Chaiperson_Contact_No: "+9779815523950",
Secretary_Name: "ranja",
Secretary_Contact_No: "+9779813523950",
Area: 784.0,
Total_Households: 422,
Total_Population: 32,
Total_Male_Population: 224,
Total_Female_Population: 124,
Website: "http://madhyapurthimimun.gov.np/en",
Email: "info@madhyapurthimimun.gov.np",
}
这是urls.py
from django.contrib import admin
from django.conf.urls import url
from django.urls import path, include # add this
from rest_framework import routers # add this
from formback import views
from rest_framework.urlpatterns import format_suffix_patterns
router = routers.DefaultRouter() # add this
router.register(r'form', views.FormView, 'Form')
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/', include(router.urls)),
url(r'^getform/', views.formAPI.as_view()),
]
这是serializers.py
from rest_framework import serializers
from .models import Form
class FormSerializer(serializers.ModelSerializer):
class Meta:
model = Form
fields = '__all__'
提前谢谢