Skip to content
Advertisement

Django json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I’m tyring to use the django login functionality, but ever since i added the login and sign_up route, I started getting this error.

Anytime i try to access the sign_up or login route, I kept getting this error…json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Traceback (most recent call last):
  File "C:UsersJOSHUAAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocorehandlersexception.py", line 34, in inner
    response = get_response(request)
  File "C:UsersJOSHUAAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocorehandlersbase.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:UsersJOSHUAAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocorehandlersbase.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:UsersJOSHUADownloadsPython stuffHarvard lessonsLesson 7project3Pizza-project3ordersviews.py", line 51, in get_item
    item_details_str = json.loads(item_details)
  File "C:UsersJOSHUAAppDataLocalProgramsPythonPython38-32libjson__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:UsersJOSHUAAppDataLocalProgramsPythonPython38-32libjsondecoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:UsersJOSHUAAppDataLocalProgramsPythonPython38-32libjsondecoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I don’t understand why the get_item function is interfering with the other functions. This is my views.py

def sign_up(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']

        user = User.objects.create_user(username = username, password = password)
        user.save()
        
        return HttpResponseRedirect(reverse(index))
    else:
        return render(request, "orders/register.html")

def login_view(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username = username, password = password)
        if user is not None:
            login(request, user)
            return HttpResponseRedirect(reverse(index))
        else:
            return render(request, "orders/login.html")
    else:
        return render(request, "orders/login.html")

def get_item(request, item_details):
    #Using ajax requests
    if request.method == 'GET' and request.is_ajax:
        #Getting the menu the item belongs too
        #converting item_details to string from json
        item_details_str = json.loads(item_details)
        menu_name = Menu.objects.get(name = item_details_str['menu_name'])

        item_data = menu_name.item.get(name = item_details_str['item_name'])
        print(item_data)


        item_price = item_data.price.all()
        print(Item_price)

        add_on = item_data.add_on.all()
        print(add_on)

        size = item_data.price.all()

        context = {
            'item_price': item_price,
            'add_on': add_on
        }
        print(context)
        context_json = serializers.serialize("json", [item_data, *item_price, *add_on])
        

        
        return HttpResponse(context_json, content_type ="application/json")
    else:
        return HttpResponse('unsuccessful')

This is my urls.py file

from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("sign_up", views.sign_up, name = 'sign_up'),
    path("login", views.login_view, name = 'login'),
    path("<str:item_details>", views.get_item, name = 'get_item')
]

Advertisement

Answer

You are trying to load item details from the request body, but there is no body.

 item_details_str = json.loads(item_details)

You should check to see if the body is empty

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement