django TypeError:quote_from_bytes()带有json

时间:2019-01-19 11:59:48

标签: json django

第一次问这里,环顾四周后,仍然卡住了 试图用Django实施tranzila付款方式,这很容易 库存到发送产品购物车 我是什么意思 我的网站给了我一个购物车,客户可以一起购买几种不同的产品,我想将此数据发送到tranzila tranzila将其接收为rawurlencoded json 进行编码后,出现TypeError:quote_from_bytes()预期的字节数

我的代码:

def cart_detail(request, total=0, counter=0, cart_items=None):
json_purchase_data = None
cart_data = []
try:
    # get session key
    cart = Cart.objects.get(cart_id=_cart_id(request))
    cart_items = CartItem.objects.filter(cart=cart, active=True)
    for cart_item in cart_items:
        # calculate total
        total += (cart_item.product.price * cart_item.quantity)
        # Item counter
        counter += cart_item.quantity
        # cart items for tranzila
        json_purchase = json.dumps([{"product_name": str(cart_item.product),
                                     "product_quantity": int(cart_item.quantity),
                                     "product_price": int(cart_item.product.price)
                                     }])
        cart_data.append(json_purchase)
    print(cart_data)
except ObjectDoesNotExist:
    pass
# tranzila Code
thtk = create_thtk(total).split("=")[1]
dc_disable = create_dc_disable()
json_purchase_data = quote(cart_data)
if request.method == 'POST':
    form = UserData(request.POST)
else:
    form = UserData()
context = {
    'cart_items': cart_items,
    'total': total,
    'counter': counter,
    'terminal_name': terminal_name,
    'thtk': thtk,
    'dc_disable': dc_disable,
    'form': form,
    'json_purchase_data': json_purchase_data
}
return render(request, 'cart/cart.html', context)

我需要帮助,非常感谢任何帮助

回溯:

    Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/cart/

Django Version: 2.1.5
Python Version: 3.6.7
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'shop.apps.ShopConfig',
 'search_app.apps.SearchAppConfig',
 'cart.apps.CartConfig',
 'order.apps.OrderConfig',
 'crispy_forms',
 'phonenumber_field']
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:

File "/home/lev/PycharmProjects/tranzila_shop/perfectshop/venv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/lev/PycharmProjects/tranzila_shop/perfectshop/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "/home/lev/PycharmProjects/tranzila_shop/perfectshop/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/lev/PycharmProjects/tranzila_shop/perfectshop/cart/views.py" in cart_detail
  95.     json_purchase_data = quote(cart_data)

File "/usr/lib/python3.6/urllib/parse.py" in quote
  789.     return quote_from_bytes(string, safe)

File "/usr/lib/python3.6/urllib/parse.py" in quote_from_bytes
  814.         raise TypeError("quote_from_bytes() expected bytes")

Exception Type: TypeError at /cart/
Exception Value: quote_from_bytes() expected bytes

0 个答案:

没有答案