【问题标题】:Django json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)Django json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)
【发布时间】:2020-11-11 06:45:23
【问题描述】:

我很想使用 django 登录功能,但是自从我添加了 login 和 sign_up 路由后,我开始收到此错误。

每当我尝试访问 sign_up 或登录路由时,我都会收到此错误...json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Traceback (most recent call last):
  File "C:\Users\JOSHUA\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\JOSHUA\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\JOSHUA\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\JOSHUA\Downloads\Python stuff\Harvard lessons\Lesson 7\project3\Pizza-project3\orders\views.py", line 51, in get_item
    item_details_str = json.loads(item_details)
  File "C:\Users\JOSHUA\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\JOSHUA\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\JOSHUA\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.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)

我不明白为什么 get_item 函数会干扰其他函数。 这是我的意见。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')

这是我的 urls.py 文件

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')
]

【问题讨论】:

  • 错误在第 51 行 views.py 中。你能给更多的代码吗?网址和视图

标签: python json python-3.x django


【解决方案1】:

是你导入的JSON包的问题。

试试这个:

import json

并删除您用于导入 JSON 包的另一个导入。

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 2017-05-09
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    相关资源
    最近更新 更多