K
K
kirill novgorodtsev2020-06-21 09:21:26
Python
kirill novgorodtsev, 2020-06-21 09:21:26

Error No data provided, 400 from Django server how to fix?

I am developing a bkend, POST GET and DELETE (but not PUT) requests work (I check everything in Postman), I don’t understand what can cause such an error, I authorize
InnVk.png

in the body and pass
u1Xwm.png

urls:

from django.urls import path
from bkend.views import *

from bkend.models import *
app_name = "bkend"

urlpatterns = [
    path('adresses/', Adresses.as_view()),
    path('servises/', Servises.as_view()),
    path('prices/', Prices.as_view()),
    path('applications/', Applications.as_view()),
    path('adresses/<int:pk>', Adresses.as_view()),
    path('servises/<int:pk>', Servises.as_view()),
    path('prices/<int:pk>', Prices.as_view()),
    path('applications/<int:pk>', Applications.as_view()),
]


serializer:

from rest_framework import serializers
from django.contrib.auth.models import User

from bkend.models import *


class UserSerializer(serializers.ModelSerializer):
    """Сериализация пользователя"""
    class Meta:
        model = User
        fields = ("id", "username")

class AdressSerializers(serializers.ModelSerializer):
    """Сериализация adresa """
    user = UserSerializer()
    adres = serializers.CharField(max_length=150)
    number_phon = serializers.CharField(max_length=150)
    x_re = serializers.FloatField(default=0)
    y_re = serializers.FloatField(default=0)
    start = serializers.CharField(max_length=150)
    stop = serializers.CharField(max_length=150)
    idi = serializers.IntegerField(default=0)

    def update(self, instance, validated_data):
        instance.user = validated_data.get('user', instance.user)
        instance.adres = validated_data.get('adres', instance.adres)
        instance.number_phon = validated_data.get('number_phon', instance.number_phon)
        instance.x_re = validated_data.get('x_re', instance.x_re)
        instance.y_re = validated_data.get('y_re', instance.y_re)
        instance.start = validated_data.get('start', instance.start)
        instance.stop = validated_data.get('stop', instance.stop)
        instance.idi = validated_data.get('idi', instance.idi)

        instance.save()
        return instance

    class Meta:
        model = Adress
        fields = ("user","adres", "number_phon", "x_re", "y_re", "start", "stop", "idi")


class ServiseSerializers(serializers.ModelSerializer):
    user = UserSerializer()
    idi = serializers.IntegerField(default=0)
    serviz = serializers.CharField(max_length=150)
    coment = serializers.CharField(max_length=150)
    tip = serializers.CharField(max_length=150)
    popularity = serializers.IntegerField(default=0)


    def update(self, instance, validated_data):
        instance.user = validated_data.get('user', instance.user)
        instance.idi = validated_data.get('idi', instance.idi)
        instance.serviz = validated_data.get('serviz', instance.serviz)
        instance.coment = validated_data.get('coment', instance.coment)
        instance.tip = validated_data.get('tip', instance.tip)
        instance.popularity = validated_data.get('popularity', instance.popularity)

        instance.save()
        return instance


    class Meta:
        model = Service
        fields = ("user","idi", "serviz", "coment", "tip", "popularity")


class PriceSerializers(serializers.ModelSerializer):
    user = UserSerializer()
    idi = serializers.IntegerField(default=0)
    tip = serializers.CharField(max_length=150)
    idi_price = serializers.IntegerField(default=0)

    def create(self, validated_data):
        return Price.objects.create(**validated_data)

    def update(self, instance, validated_data):
        # instance.user = validated_data.get('user', instance.user)
        instance.idi = validated_data.get('idi', instance.idi)
        instance.tip = validated_data.get('tip', instance.tip)
        instance.idi_price = validated_data.get('idi_price', instance.idi_price)

        instance.save()
        return instance



    class Meta:
        model = Price
        fields = ("id","user","idi", "tip", "idi_price")

class ApplicationSerializers(serializers.ModelSerializer):
    user = UserSerializer()
    idi = serializers.IntegerField(default=0)
    coment = serializers.CharField(max_length=150)
    marka = models.CharField(max_length=150)
    symma = serializers.CharField(max_length=150)
    sevises = serializers.CharField(max_length=150)
    phone = serializers.CharField(max_length=150)
    size = serializers.CharField(max_length=150)
    adres = serializers.CharField(max_length=150)
    number_phon = serializers.CharField(max_length=150)
    date_time_start = serializers.DateTimeField()
    date_and_time = serializers.CharField(max_length=150)


    def update(self, instance, validated_data):
        instance.user = validated_data.get('user', instance.user)
        instance.adres = validated_data.get('adres', instance.adres)
        instance.number_phon = validated_data.get('number_phon', instance.number_phon)
        instance.coment = validated_data.get('coment', instance.coment)
        instance.marka = validated_data.get('marka', instance.marka)
        instance.symma = validated_data.get('symma', instance.symma)
        instance.sevises = validated_data.get('sevises', instance.sevises)
        instance.idi = validated_data.get('idi', instance.idi)
        instance.phone = validated_data.get('phone', instance.phone)
        instance.size = validated_data.get('size', instance.size)
        instance.date_time_start = validated_data.get('date_time_start', instance.date_time_start)
        instance.date_and_time = validated_data.get('date_and_time', instance.date_and_time)

        instance.save()
        return instance



    class Meta:
        model = Application
        fields = ("user","idi", "coment", "marka", "symma", "sevises", "phone", "size", "adres", "number_phon", "date_time_start", "date_and_time")


views:

from django.shortcuts import render
from rest_framework.generics import get_object_or_404

# Create your views here.
from typing import Type, List

from django.shortcuts import render
from django.contrib.auth.models import User
# Create your views here.
from django.db.models import Q
from rest_framework.permissions import IsAuthenticated

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import permissions

from django.contrib.auth.models import User

from bkend.models import *
from bkend.serializers import *




class Adresses(APIView):
    """Комнаты чата"""
    # permission_classes: List[Type[IsAuthenticated]] = [permissions.IsAuthenticated, ]


    def get(self, request):
        permission_classes = [permissions.AllowAny, ]
        adds = Adress.objects.all()
        serializer = AdressSerializers(adds, many=True)
        return Response({"adds": serializer.data})

    def post(self, request):
        permission_classes = [permissions.IsAuthenticated, ]
        user = request.data.get("user")
        try:
            Adresses.objects.create(adres=request.adres)
            return Response(status=201)
        except:
            return Response(status=400)


    def put(self, request, pk):
        saved_article = get_object_or_404(Adress.objects.all(), pk=pk)
        data = request.data.get('adress')
        serializer = AdressSerializer(instance=saved_adress, data=data, partial=True)
        if serializer.is_valid(raise_exception=True):
            adress_saved = serializer.save()
        return Response({
            "success": "Adress '{}' updated successfully".format(adress_saved.title)
        })


    def delete(self, request, pk):
        # Get object with this pk
        adres = get_object_or_404(Adress.objects.all(), pk=pk)
        adres.delete()
        return Response({
            "message": "Adres with id `{}` has been deleted.".format(pk)
        }, status=204)


class Prices(APIView):
    def get(self, request):
        permission_classes = [permissions.AllowAny, ]
        adds = Price.objects.all()
        serializer = PriceSerializers(adds, many=True)
        return Response({"adds": serializer.data})

    def post(self, request):
        permission_classes = [permissions.IsAuthenticated, ]

        user = request.data.get("user")
        try:
            Price.objects.create(adres=request.adres)
            return Response(status=201)
        except:
            return Response(status=400)


    def put(self, request, pk):
        saved_price = get_object_or_404(Price.objects.all(), pk=pk)
        data = request.data.get('price')
        serializer = PriceSerializers(instance=saved_price, data=data, partial=True)
        if serializer.is_valid(raise_exception=True):
            price_saved = serializer.save()
        return Response({
            "success": "Price '{}' updated successfully".format(price_saved.title)
        })

    def delete(self, request, pk):
        # Get object with this pk
        pric = get_object_or_404(Price.objects.all(), pk=pk)
        pric.delete()
        return Response({
            "message": "Price with id `{}` has been deleted.".format(pk)
        }, status=204)


I check everything so far on the Prices class

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2020-06-21
@cmyser

data = request.data.get('price')

Do you have a price field in your post? I see that there immediately separate fields of the model go

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question