Facing this error while trying to get request invalid literal for int() with base 10: b'10 00:00:00'

I was trying to do get request to the url http://127.0.0.1:8000/books/list/ but im now facing an erorr invalid literal for int() with base 10`

#my views.py

from django.shortcuts import render
from book_api.models import Book
from django.http import JsonResponse
from book_api.serializer import BookSerializer
from rest_framework.response import Response
from rest_framework.decorators import api_view

# Create your views here.
@api_view(['GET'])
def book_list(request):
    books=Book.objects.all()
    serializer=BookSerializer(books,many=True)
    return Response(serializer.data)

@api_view(['POST'])
def book_create(request):
    serializer=BookSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data)
    else:
        return Response(serializer.errors)
        


#my serializer.py

from rest_framework import serializers
from book_api.models import Book


class BookSerializer(serializers.Serializer):
    id=serializers.IntegerField(read_only=True)
    title=serializers.CharField()
    number_of_pages=serializers.IntegerField()
    publish_date=serializers.DateField()
    quantity=serializers.IntegerField()
    
    def create(self,data):
        return Book.objects.create(**data)


#book_api.urls
from django.contrib import admin
from django.urls import path
from book_api.views import book_list,book_create


urlpatterns = [
    path('',book_create),
    path('list/',book_list)


#
book/urls.py
"""BOOK URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('books/',include('book_api.urls'))
]
#the directory tree


├───BOOK
│   └───__pycache__
└───book_api
    ├───migrations
    │   └───__pycache__
    └───__pycache__

i just need help navigating the bugs inside my code

The full traceback would help a lot, but from what I can see this error message is being raised because the data being sent in the GET request to the URL http://127.0.0.1:8000/books/list/ is not valid for the variables of IntegerField perhaps. They are expecting to receive a valid integer value, but it's getting an invalid string value instead. Maybe check what is the type of data being sent to the "number of pages" and "quantity" fields

Back to Top