0

我在 Ubuntu 12.04 上使用 Django 1.4 和 Python 2.7。

我有一个模板应该显示一个产品和每个产品的产品功能列表,出于某种原因,这些功能没有显示在模板中。

这是视图:

@login_required
def view_products(request):
    """
    ..  function:: view_products()

        View the Products

        :param request: Django Request object
    """

    data = { 'user' : request.user }
    if (request.user.is_authenticated() and request.user.is_superuser):
        products = Products.objects.all()

        add_feature_form = rsb.forms.AddProductFeatureForm();
        data.update({ 'form' : add_feature_form })
        data.update({ 'products' : products })
        data.update(csrf(request))

        return render_to_response("view_products.html", data)

    return render_to_response("index.html", data)

以下是使用产品功能的模板部分:

<table>
    {% for product in products %}
        <tr>
            <td align="right">Product Name:</td><td>{{ product.name }}</td>
        </tr>
        <tr>
            <td align="right">Price:<br /></td><td>${{ product.price }}</td>
        </tr>
        <tr>
            <ul>
            {% for productfeature in product.productfeature_set.all %}
                <form action="/removeProductFeature/" method="post">{% csrf_token %}
                <li>
                    {{ productfeature.feature }}
                    <input type="hidden" name="feature" value={{ productfeature.feature }}>
                    <input type="hidden" name="product_id" value={{ product.id }}>
                    <label class="formlabel">&nbsp;</label><input type="submit" value="Remove  &#9658;">
                </tr>
                </form>
            {% endfor %}
            </ul>
        </tr>
        <tr>
            <form action="/addProductFeature/" method="post">{% csrf_token %}
            <table>
                <tr>
                    <td align="right"><label class="formlabel">Add Feature:<br /></label></td><td>{{ form.feature }}</td>
                </tr>
                <input type="hidden" name="product_id" value={{ product.id }}>
                <tr>
                    <td align="right"><label class="formlabel">&nbsp;</label></td><td><input type="submit" value="Add  &#9658;"></td>
                </tr>
            </form>
            </table>
        </tr>
   {% endfor %}
</table>

基本上这个模板应该向你展示一个产品。每个功能都将列在其下方,并带有“删除”该功能的选项。然后,在底部,一个允许您添加附加功能的字段。

现有功能根本不显示。关于我可能做错了什么的任何建议?

更新 1:

我错过了s模板中的一个。 product.productfeatures_set.all不是product.productfeature_set.all。我可以走了。谢谢大家!

4

2 回答 2

3

请不要这样做:

product_features = []
for product in products:
    features = ProductFeatures.objects.filter(product = product)
    product_features.append(features)
    product.features = product_features

相反,只需将您的 products 变量传递给模板上下文。

在模板中做:

{% for product in products %}
    Product id: {{ product.pk }}
    {% for productfeature in product.productfeature_set.all %}
        {{ productfeature.feature }}
    {% endfor %}
{% endfor %}

你会问什么productfeature_set(或者,我希望你会问:D),这是一个非常好的问题。不要惊慌,这一切都有记录

现在,这将导致子查询产生。解决方案是使用prefetch_related。但我认为你暂时不必担心这个:)

于 2012-10-24T12:21:25.257 回答
1

是的,你正在尝试实现一些已经内置到 Django 中的东西!

您应该在 django 的反向关系中使用构建。

# give an Product instance product
# this should work
product.productfeature_set.all()

可以从模板 a 访问product.productfeature_set.all 并且可以迭代。

于 2012-10-24T12:23:29.640 回答