试图将django-oscar产品传递给自定义模板

时间:2018-12-14 11:02:59

标签: python django django-oscar

我正在使用django-oscar应用程序的分叉版本(具有自定义模型)来制作自定义模板。

我正尝试在产品表中显示所有产品的列表。我查看了django-oscar模板,但是由于它们依赖于许多自定义的tempaltetag,我发现重写所有内容以与模型一起使用太复杂了。

这就是我的观点。

def product(request):
    template = loader.get_template('/home/mysite/django_sites/my_site/main_page/templates/main_page/product.html')
    prodlist = Product.objects.all()
    return HttpResponse(template.render({}, request), context={'prodlist': prodlist})

以及我在模板中尝试显示的代码

{% for instance in prodlist%}
    <li>{{ instance.name }}</li>
{% endfor %}

但是,这给了我一个错误

TypeError at /product/ 
__init__() got an unexpected keyword argument 'context'

/ product对应于我的urls.py中的产品视图

根据以下教程并查看其他答案,这是我最好的猜测。我怎么了?

1 个答案:

答案 0 :(得分:1)

HttpResponse没有context arg。 似乎您需要在render中添加上下文。

尝试:

context={'prodlist': prodlist}
return HttpResponse(template.render(context))