首页 > 解决方案 > 当注册用户尝试在购物车中添加产品时,它会显示内部服务器错误:/update_item/

问题描述

当来宾用户添加产品时,它可以正常工作,但是当注册用户尝试在购物车中添加产品时,它会出错。

Internal Server Error: /update_item/
    Traceback (most recent call last):
  File "C:\Users\varuni\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\varuni\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\varuni\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
TypeError: updateItem() missing 1 required positional argument: 'customer'

这是views.py中的updateItem:

def updateItem(request, customer):
    data = json.loads(request.body)
    productId = data['productId']
    action = data['action']
    
    print('Action: ',action)
    print('productId: ',productId)
    
    product = Product.objects.get(id = productId)
    order, created = Order.objects.get_or_create(customer = request.user.customer, complete= False)
    
    orderItem, created = OrderItem.objects.get_or_create(order= order, product= product)
    
    if action=='add':
        orderItem.quantity = (orderItem.quantity + 1)
    elif action == 'remove' :
        orderItem.quantity = (orderItem.quantity - 1)
    
    orderItem.save()
    
    if orderItem.quantity <= 0:
        orderItem.delete()
    
    return JsonResponse('item added', safe=False)

这是我的 store.html,其中定义了添加到购物车按钮的操作:

{% extends 'store/main.html' %}
{% load static %}

{% block content %}
    <div class="row">
        {% for prod in products %}
        <div class="col-lg-4">
            <img class="thumbnail" src="{{prod.imageURL}}">
            <div class="box-element product">
                <h6><strong>{{prod.name}}</strong></h6>
                <hr>
                <button data-product={{prod.id}} data-action="add" class="btn btn-outline-secondary add-btn update-cart">Add to Cart</button>
                <a class="btn btn-outline-success" href="#">View</a>
                <h4 class="d-inline float-right">{{prod.price}} Rs.</h4>
            </div>
        </div>
        {% endfor %}
    </div>
{% endblock content %}

这是我的 urls.py:

urlpatterns = [
    path('', views.store, name='store'),
    path('cart/', views.cart, name='cart'),
    path('register/', views.registerPage, name='register'),
    path('login', views.loginPage, name='login'),
    path('logout', views.logoutPage, name='logout'),
    path('checkout/', views.checkout, name="checkout"),
    path('update_item/', views.updateItem, name="update_item"),
    path('process_order/', views.processOrder, name="process_order"),
]

如果需要任何其他信息,请告诉我。

标签: javascriptpythondjangoe-commerceinternal-server-error

解决方案


推荐阅读