Im trying to build an api with Django. I think i build it correctly but from, for example Post-request data, i get as the field values "none" Maybe you can help me to solve this problem :)
Heres the api:
def apiRequests(request):
if request.method == "POST":
print(request.POST.get("name"))
print(request.POST.get("publisher"))
print(request.POST.get("price"))
#Game.objects.create(name= request.POST.get('name'),publisher = request.POST.get("publisher"),price = request.POST.get("price"))
elif request.method == "GET":
response = Game.objects.filter(name = request.GET.get("name"))
return render(request,"apirequests.html")
As you may see i'm printing out the data i receive. This looks like this:
None
None
None
[26/Aug/2022 08:40:09] "POST /api/ HTTP/1.1" 200 263
Here's the model class:
class Game(models.Model):
name = models.CharField(max_length=200)
publisher = models.CharField(max_length=200)
price = models.BigIntegerField()
def __str__(self):
return str(self.id) " " self.name
And here is the data I'm sending as a Post-request from Postman to the api:
{
"name": "FarCry",
"publisher": "EA",
"price": "35.66"
}
I think i should say that i got Problems with the CsrfViewMiddleware-Token so i commented it out in the settings.py, maybe there is the problem. Thanks for helping
CodePudding user response:
If you are using Postman, send the body as form-data.