【问题标题】:How to only render Products with a certain title?如何只渲染具有特定标题的产品?
【发布时间】:2018-05-24 14:59:21
【问题描述】:

所以我终于能够在主页上呈现我的产品。 但是当我尝试按品牌名称列出产品时(在仪表板中创建产品时输入“标题”),因此我只想呈现具有特定标题名称的某些产品。 我的猜测是这样做(假设我输入的标题名称是 uniqlo):

        {% for product in products %}
                    {% render_product product.product_title.uniqlo %}
        {% endfor %}

但不幸的是,没有渲染任何内容。我尝试了许多其他可能性,但到目前为止都没有运气。

我还尝试通过查看 shell 来了解产品是如何保存的,但是无论我输入什么 from..import..Product.objects.all() 导致 Product 都没有定义。

【问题讨论】:

    标签: django django-templates django-oscar


    【解决方案1】:

    询问不清楚。你想总是过滤那个uniqlo标题吗?还是针对某些特定的(登陆/主)页面?

    无论如何,一个简单的方法是(基于你已经尝试过的)-

    {% for product in products %}
        {% if 'uniqlo' in product.title %}
            {% render_product product %}
        {% endif %}
    {% endfor %}
    

    另外,对于 shell 导入,请尝试

    from oscar.core.loading import get_model
    Product = get_model('catalogue', 'Product')
    

    如果您尝试仅在登录/主页中过滤标题,则可以在将产品传递给模板之前在视图本身中过滤产品 -

    products = Product.objects.filter(title__icontains='uniqlo')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      相关资源
      最近更新 更多