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)
JavaScript
x
18
18
1
Traceback (most recent call last):
2
File "C:UsersJOSHUAAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocorehandlersexception.py", line 34, in inner
3
response = get_response(request)
4
File "C:UsersJOSHUAAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocorehandlersbase.py", line 115, in _get_response
5
response = self.process_exception_by_middleware(e, request)
6
File "C:UsersJOSHUAAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocorehandlersbase.py", line 113, in _get_response
7
response = wrapped_callback(request, *callback_args, **callback_kwargs)
8
File "C:UsersJOSHUADownloadsPython stuffHarvard lessonsLesson 7project3Pizza-project3ordersviews.py", line 51, in get_item
9
item_details_str = json.loads(item_details)
10
File "C:UsersJOSHUAAppDataLocalProgramsPythonPython38-32libjson__init__.py", line 357, in loads
11
return _default_decoder.decode(s)
12
File "C:UsersJOSHUAAppDataLocalProgramsPythonPython38-32libjsondecoder.py", line 337, in decode
13
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
14
File "C:UsersJOSHUAAppDataLocalProgramsPythonPython38-32libjsondecoder.py", line 355, in raw_decode
15
raise JSONDecodeError("Expecting value", s, err.value) from None
16
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
17
18
I don’t understand why the get_item function is interfering with the other functions. This is my views.py
JavaScript
1
59
59
1
def sign_up(request):
2
if request.method == 'POST':
3
username = request.POST['username']
4
password = request.POST['password']
5
6
user = User.objects.create_user(username = username, password = password)
7
user.save()
8
9
return HttpResponseRedirect(reverse(index))
10
else:
11
return render(request, "orders/register.html")
12
13
def login_view(request):
14
if request.method == 'POST':
15
username = request.POST['username']
16
password = request.POST['password']
17
user = authenticate(request, username = username, password = password)
18
if user is not None:
19
login(request, user)
20
return HttpResponseRedirect(reverse(index))
21
else:
22
return render(request, "orders/login.html")
23
else:
24
return render(request, "orders/login.html")
25
26
def get_item(request, item_details):
27
#Using ajax requests
28
if request.method == 'GET' and request.is_ajax:
29
#Getting the menu the item belongs too
30
#converting item_details to string from json
31
item_details_str = json.loads(item_details)
32
menu_name = Menu.objects.get(name = item_details_str['menu_name'])
33
34
item_data = menu_name.item.get(name = item_details_str['item_name'])
35
print(item_data)
36
37
38
item_price = item_data.price.all()
39
print(Item_price)
40
41
add_on = item_data.add_on.all()
42
print(add_on)
43
44
size = item_data.price.all()
45
46
context = {
47
'item_price': item_price,
48
'add_on': add_on
49
}
50
print(context)
51
context_json = serializers.serialize("json", [item_data, *item_price, *add_on])
52
53
54
55
return HttpResponse(context_json, content_type ="application/json")
56
else:
57
return HttpResponse('unsuccessful')
58
59
This is my urls.py file
JavaScript
1
12
12
1
from django.urls import path
2
3
from . import views
4
5
urlpatterns = [
6
path("", views.index, name="index"),
7
path("sign_up", views.sign_up, name = 'sign_up'),
8
path("login", views.login_view, name = 'login'),
9
path("<str:item_details>", views.get_item, name = 'get_item')
10
]
11
12
Advertisement
Answer
You are trying to load item details from the request body, but there is no body.
JavaScript
1
2
1
item_details_str = json.loads(item_details)
2
You should check to see if the body is empty