使用渲染时的值错误(django 1.7.1)

时间:2015-02-25 09:36:43

标签: django django-views

我有错误

Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/user/accounter/apps/accounter/views.py" in unit_view
  24.     return render(request, 'accounter/unit.html', context)
File "/usr/local/lib/python2.7/dist-packages/django/shortcuts.py" in render
  48.     return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in render_to_string
  177.     with context_instance.push(dictionary):
File "/usr/local/lib/python2.7/dist-packages/django/template/context.py" in push
  54.         return ContextDict(self, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/template/context.py" in __init__
  19.         super(ContextDict, self).__init__(*args, **kwargs)

Exception Type: ValueError at /accounter/1/
Exception Value: dictionary update sequence element #0 has length 3; 2 is required

我的观点源代码:

class ProductListView(generic.ListView):
    model = Product

    def get_queryset(self):
            return Product.objects.all()

def unit_view(request, product_id):
    context = Context({
        'product': Product.objects.get(pk=product_id).name,
        'ship_units': ShipUnit.objects.filter(product = product_id),
        'shop_units': ShopUnit.objects.filter(product = product_id),
    })
    return render(request, 'accounter/unit.html', context)

我无法弄清楚是什么问题。我已经阅读了有关使用渲染和上下文的文档,以及所有示例。但是找不到我的错误。

1 个答案:

答案 0 :(得分:1)

render是一种捷径;它不需要一个Context对象,它需要一个简单的字典。

context = {
    'product': Product.objects.get(pk=product_id).name,
    'ship_units': ShipUnit.objects.filter(product = product_id),
    'shop_units': ShopUnit.objects.filter(product = product_id),
}