Skip to main content
 首页 » 编程设计

python之无法从 request.session 中按键获取值;我用 Django

2024年09月03日31lyhabc

我正在尝试使用 django 进行网上购物。但是我遇到了一些问题。在制作购物车期间,我在 views.py 中收到错误消息“局部变量‘cart_id’在赋值之前被引用”。 这是这个文件:

def cart_view(request): 
    try: 
        cart_id = request.session['cart_id'] 
        cart = Cart.objects.get(id=cart_id) 
        request.session['total'] = cart.items.count() 
    except: 
        cart = Cart() 
        cart.save() 
        cart_id = cart_id 
        request.session['cart_id'] = cart_id 
        cart = Cart.objects.get(id=cart_id) 
 
    categories = Category.objects.all() 
    return render(request, 'cart.html', locals())  

这是回溯

Traceback: 
 
File "C:\Users\tankr\Django_projects\django_shop\ecomapp\views.py" in product_view 
  13.       cart_id = request.session['cart_id'] 
 
File "C:\Users\tankr\Django_projects\django_shop\eComEnv\lib\site-packages\django\contrib\sessions\backends\base.py" in __getitem__ 
  54.         return self._session[key] 
 
During handling of the above exception ('cart_id'), another exception occurred: 
 
File "C:\Users\tankr\Django_projects\django_shop\eComEnv\lib\site-packages\django\core\handlers\exception.py" in inner 
  34.             response = get_response(request) 
 
File "C:\Users\tankr\Django_projects\django_shop\eComEnv\lib\site-packages\django\core\handlers\base.py" in _get_response 
  115.                 response = self.process_exception_by_middleware(e, request) 
 
File "C:\Users\tankr\Django_projects\django_shop\eComEnv\lib\site-packages\django\core\handlers\base.py" in _get_response 
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs) 
 
File "C:\Users\tankr\Django_projects\django_shop\ecomapp\views.py" in product_view 
  19.       cart_id = cart_id 
 
Exception Type: UnboundLocalError at /product/macbook-pro/ 
Exception Value: local variable 'cart_id' referenced before assignment 

然后我尝试通过将 global cart_id 添加到函数 cart_view 来解决它,但又出现了另一个错误:name 'cart_id' is not defined。 这是新的追溯:

Traceback: 
 
File "C:\Users\tankr\Django_projects\django_shop_2\ecomapp\views.py" in product_view 
  14.       cart_id = request.session['cart_id'] 
 
File "C:\Users\tankr\Django_projects\django_shop_2\eComEnv\lib\site-packages\django\contrib\sessions\backends\base.py" in __getitem__ 
  54.         return self._session[key] 
 
During handling of the above exception ('cart_id'), another exception occurred: 
 
File "C:\Users\tankr\Django_projects\django_shop_2\eComEnv\lib\site-packages\django\core\handlers\exception.py" in inner 
  34.             response = get_response(request) 
 
File "C:\Users\tankr\Django_projects\django_shop_2\eComEnv\lib\site-packages\django\core\handlers\base.py" in _get_response 
  115.                 response = self.process_exception_by_middleware(e, request) 
 
File "C:\Users\tankr\Django_projects\django_shop_2\eComEnv\lib\site-packages\django\core\handlers\base.py" in _get_response 
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs) 
 
File "C:\Users\tankr\Django_projects\django_shop_2\ecomapp\views.py" in product_view 
  20.       cart_id = cart_id 
 
Exception Type: NameError at /product/iphone-xr-128gb/ 
Exception Value: name 'cart_id' is not defined 

请您参考如下方法:

cart_id = cart_id 行没有意义,我想你想使用 cart.id,或者更安全的 cart .pk:

def cart_view(request): 
    try: 
        cart_id = request.session['cart_id'] 
        cart = Cart.objects.get(id=cart_id) 
    except (KeyError, Cart.DoesNotExist): 
        cart = Cart.objects.create() 
        request.session['cart_id'] = cart_id = <b>cart.pk</b> 
        request.session['total'] = 0 
    else: 
        request.session['total'] = cart.items.count() 
    categories = Category.objects.all() 
    return render(request, 'cart.html', locals())

不过,请不要使用locals()。从软件设计的角度来看,这不是好的做法。这确实意味着您不知道传递给模板的是什么。如果您故意创建一个与上下文处理器中的变量同名的变量,您可能会得到意想不到的行为。