【问题标题】:Trying to pass django-oscar products to custom template试图将 django-oscar 产品传递给自定义模板
【发布时间】:2023-04-02 08:57:02
【问题描述】:

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

我试图在产品表中显示所有产品的列表,这只是开始。我查看了 django-oscar 模板,但由于它们依赖于许多自定义模板标签,我发现重写所有内容以使用我的模型太复杂了。

这就是我的views.py:

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 中的产品视图

这是我从以下教程和其他答案中得出的最佳猜测。我哪里错了?

【问题讨论】:

    标签: python django django-oscar


    【解决方案1】:

    HttpResponse 没有 context 参数。 似乎您需要将上下文添加到render

    试试:

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

    【讨论】:

    • 嗯,谢谢!虽然现在我在 /product/ ==newline== column catalogue_product.collection_id does not exist ==newline== LINE 1: ..._updated", "catalogue_product"."is_discountable", "catalogue...跨度>
    • 您运行迁移了吗? python manage.py makemigrationspython manage.py migrate ?
    • 我以前做过。我现在再次运行 makemigrations,它说没有检测到任何变化。与我如何分叉应用程序有关?- 运行,运行迁移并且我遇到错误,将解决这个问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 2023-03-20
    • 2014-05-13
    • 2014-05-11
    • 1970-01-01
    • 2011-11-10
    相关资源
    最近更新 更多