javascript - 当注册用户尝试在购物车中添加产品时,它会显示内部服务器错误:/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"),
]
如果需要任何其他信息,请告诉我。
解决方案
推荐阅读
- ios - ios swift json数据到字典
- javascript - 如何使 tableRow 可点击?我在网上看过,但是没有用
- javascript - 我的 AngularJs 应用程序中的路由问题
- swift - 我无法在 Swift 的类中创建空数组属性
- python - 使用“pathos.pools.ProcessPool”锁定的规范方法是什么?
- python - 如果第 4 行没有记录,则删除文件,PYTHON
- php - 防止直接 url 访问位于受保护的 php 页面内的 pdf 链接
- firebase - Flutter 和 Firebase:从 Firebase 存储返回所有图像
- java - 是否有通用代码将日期格式(时间戳)更改为数字格式
- python-3.x - rasa 初始化 rasa init --no-prompt 面临许多问题