ResponseError'zunionstore'命令的参数个数错误

时间:2017-04-10 09:02:47

标签: django python-3.x sqlite redis

欢迎朋友们,

我是Python的新手。我正在根据一本书构建Django应用程序:Django By Example Antonio Mele。简单的商店。不幸的是我有ResponseError。

当我从购物车中删除最后一件商品时:

ResponseError at /en/cart/
wrong number of arguments for 'zunionstore' command
Request Method: GET
Request URL:    http://127.0.0.1:8000/en/cart/
Django Version: 1.10.6
Exception Type: ResponseError
Exception Value:    
wrong number of arguments for 'zunionstore' command
Exception Location: /home/vader/env2/lib/python3.5/site-packages/redis/connection.py in read_response, line 574

我从Django Shell收到了这个错误回溯:

Internal Server Error: /en/cart/
Traceback (most recent call last):
  File "/home/vader/env2/lib/python3.5/site-packages/django/core/handlers/exception.py", line 42, in inner
    response = get_response(request)
  File "/home/vader/env2/lib/python3.5/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/home/vader/env2/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/vader/env2/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/vader/myshop/cart/views.py", line 40, in cart_detail
    recommended_products = r.suggest_products_for(cart_products, max_results=4)
  File "/home/vader/myshop/shop/recommender.py", line 40, in suggest_products_for
    r.zunionstore(tmp_key, keys)
  File "/home/vader/env2/lib/python3.5/site-packages/redis/client.py", line 1780, in zunionstore
    return self._zaggregate('ZUNIONSTORE', dest, keys, aggregate)
  File "/home/vader/env2/lib/python3.5/site-packages/redis/client.py", line 1795, in _zaggregate
    return self.execute_command(*pieces)
  File "/home/vader/env2/lib/python3.5/site-packages/redis/client.py", line 565, in execute_command
    return self.parse_response(connection, command_name, **options)
  File "/home/vader/env2/lib/python3.5/site-packages/redis/client.py", line 577, in parse_response
    response = connection.read_response()
  File "/home/vader/env2/lib/python3.5/site-packages/redis/connection.py", line 574, in read_response
    raise response
redis.exceptions.ResponseError: wrong number of arguments for 'zunionstore' command
[06/Apr/2017 20:08:29] "GET /en/cart/ HTTP/1.1" 500 99706

我无法找到问题的解决方案。 请帮忙

环境:

Django Version: 1.10.6
Python Version: 3.5.2
Redis 3.2.7
Sqlite 3

示例当然是:Django示例Antonio Mele - 第6节:跟踪用户操作。作者使用Django 1.8,Python 3.4和Redis 3.0.4。所以我认为这是django 10.6中的一个问题,但我不知道为什么。

https://github.com/lancelote/django_by_example

店/ recommender.py

 line 40  r.zunionstore(tmp_key, keys)

车/ views.py

 line 40  recommended_products = r.suggest_products_for(cart_products, max_results=4)

有人应对此事吗?

有人对此有任何建议吗?

更新

车/ views.py

def cart_remove(request, product_id):
    cart = Cart(request)
    product = get_object_or_404(Product, id=product_id)
    cart.remove(product)
    return redirect('cart:cart_detail')


def cart_detail(request):
    cart = Cart(request)
    for item in cart:
        item['update_quantity_form'] = CartAddProductForm(
            initial={'quantity': item['quantity'],
                     'update': True})
    coupon_apply_form = CouponApplyForm()

    r = Recommender()
    cart_products = [item['product'] for item in cart]
    recommended_products = r.suggest_products_for(cart_products, max_results=4)

    return render(request,
                  'cart/detail.html',
                  {'cart': cart,
                   'coupon_apply_form': coupon_apply_form,
                   'recommended_products': recommended_products})

detail.html

    {% for item in cart %}
        {% with product=item.product %}
        <tr>
            <td>
                <a href="{{ product.get_absolute_url }}">
                    <img src="{% if product.image %}{{ product.image.url }}{% else %}{% static "img/no_image.png" %}{% endif %}">
                </a>
            </td>
            <td>{{ product.name }}</td>
            <td>
                <form action="{% url "cart:cart_add" product.id %}" method="post">
                    {{ item.update_quantity_form.quantity }}
                    {{ item.update_quantity_form.update }}
                    <input type="submit" value="{% trans "Update" %}">
                    {% csrf_token %}
                </form>
            </td>
            <td><a href="{% url "cart:cart_remove" product.id %}">{% trans "Remove" %}</a></td>
            <td class="num">${{ item.price }}</td>
            <td class="num">${{ item.total_price }}</td>
        </tr>
        {% endwith %}
    {% endfor %}

1 个答案:

答案 0 :(得分:1)

好朋友。我解决了这个问题。

看起来这本书的作者已经忘记了一些东西。

我不知道这是不是最好的方法,但只是我改变了这个功能:

def cart_remove(request, product_id):
    cart = Cart(request)
    product = get_object_or_404(Product, id=product_id)
    cart.remove(product)
    return redirect('cart:cart_detail')

def cart_remove(request, product_id):
    cart = Cart(request)
    product = get_object_or_404(Product, id=product_id)
    cart.remove(product)
    if cart:
        return redirect('cart:cart_detail')
    return redirect('/')

这解决了我的问题,并希望这将在未来帮助你。