当我尝试在视图中(在 basket 应用程序中)设置会话值时,出现了这个奇怪的 TypeError:“产品类型的对象不是 JSON 可序列化的”。错误发生在 request.session['Hello'] = 'foo'
。
不过,这个错误不会发生在其他地方。例如,在 store 应用程序中,在 views.py 中,以下 request.session['Hello World'] = 'Alloy'
效果很好。
你知道为什么吗?
(有问题)
from django.shortcuts import render, get_object_or_404
from django.http import JsonResponse
from . basket import Basket
from store.models import Product
from discount.forms import UserDiscountForm
def basket_summary(request):
basket = Basket(request)
context = {'basket':basket}
request.session['Hello'] = 'foo'
return render(request,"store/basket_summary.html",context)
(效果很好)
from django.shortcuts import get_object_or_404, render
from requests.sessions import session
from .models import Category, Product
def home(request):
print('----------// HOME PAGE //----------')
request.session['Hello World'] = 'Alloy'
context = {}
return render(request, 'store/home.html', context)
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/basket/
Django Version: 3.2
Python Version: 3.9.4
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'store',
'account',
'basket',
'orders',
'payment',
'contact',
'address',
'discount',
'shipping']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "C:\Users\Utilisateur\Documents\Environments\monoi_django_virtualenv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Utilisateur\Documents\Environments\monoi_django_virtualenv\lib\site-packages\django\utils\deprecation.py", line 119, in __call__
response = self.process_response(request, response)
File "C:\Users\Utilisateur\Documents\Environments\monoi_django_virtualenv\lib\site-packages\django\contrib\sessions\middleware.py", line 61, in process_response
request.session.save()
File "C:\Users\Utilisateur\Documents\Environments\monoi_django_virtualenv\lib\site-packages\django\contrib\sessions\backends\db.py", line 83, in save
obj = self.create_model_instance(data)
File "C:\Users\Utilisateur\Documents\Environments\monoi_django_virtualenv\lib\site-packages\django\contrib\sessions\backends\db.py", line 70, in create_model_instance
session_data=self.encode(data),
File "C:\Users\Utilisateur\Documents\Environments\monoi_django_virtualenv\lib\site-packages\django\contrib\sessions\backends\base.py", line 114, in encode
return signing.dumps(
File "C:\Users\Utilisateur\Documents\Environments\monoi_django_virtualenv\lib\site-packages\django\core\signing.py", line 110, in dumps
return TimestampSigner(key, salt=salt).sign_object(obj, serializer=serializer, compress=compress)
File "C:\Users\Utilisateur\Documents\Environments\monoi_django_virtualenv\lib\site-packages\django\core\signing.py", line 172, in sign_object
data = serializer().dumps(obj)
File "C:\Users\Utilisateur\Documents\Environments\monoi_django_virtualenv\lib\site-packages\django\core\signing.py", line 87, in dumps
return json.dumps(obj, separators=(',', ':')).encode('latin-1')
File "c:\users\utilisateur\appdata\local\programs\python\python39\lib\json\__init__.py", line 234, in dumps
return cls(
File "c:\users\utilisateur\appdata\local\programs\python\python39\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "c:\users\utilisateur\appdata\local\programs\python\python39\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "c:\users\utilisateur\appdata\local\programs\python\python39\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
Exception Type: TypeError at /basket/
Exception Value: Object of type Product is not JSON serializable