Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何从django中的另一个模型获取图像_Html_Django_Django Models - Fatal编程技术网

Html 如何从django中的另一个模型获取图像

Html 如何从django中的另一个模型获取图像,html,django,django-models,Html,Django,Django Models,我想为我的产品模型获取图像表单图像模型,并显示购物车模板 这是我的代码: 电子商务/models.py class Product(models.Model): name = models.CharField(max_length=200) content = models.TextField() excerpt = models.TextField() price = models.DecimalField(max_digits=6, decimal_place

我想为我的产品模型获取图像表单图像模型,并显示购物车模板

这是我的代码:

电子商务/models.py

class Product(models.Model):
    name = models.CharField(max_length=200)
    content = models.TextField()
    excerpt = models.TextField()
    price = models.DecimalField(max_digits=6, decimal_places=2)
    status = models.IntegerField(default=0)
    date = models.DateTimeField(auto_now_add=True)
    quantity = models.PositiveIntegerField()
    author = models.PositiveIntegerField()
    featured_image = models.CharField(max_length=300)
    available = models.BooleanField(default=True)

    def __str__(self):
        return self.name

class Image(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    image = models.ImageField()
cart/views.py

@require_POST
def cart_add(request, product_id):
    cart = Cart(request)
    product = get_object_or_404(Product, pk=product_id)
    form = CartAddProductForm(request.POST)
    if form.is_valid():
        cd = form.cleaned_data
        cart.add(product=product,
                 quantity=cd['quantity'],
                 update_quantity=cd['update'])
    return redirect('cart:cart_detail')    

def cart_detail(request):
    cart = Cart(request)
    for item in cart:
        item['update_quantity_form'] = CartAddProductForm(initial={'quantity': item['quantity'],
                                                                   'update': True})
    return render(request, 'cart_detail.html', {'cart': cart})
cart/templates/cart.html

<tbody>
        {% for item in cart %}
            {% with product=item.product %}
            <tr>
                <td>
                    {% for product in products %} 
                        <img src="{{ product.first_image.src }}" alt="" width="auto" height="340"/>
                    {% endfor %}
                </td>
                <td>{{ product.name }}</td>
                .
                .
                .
            </tr>
            {% endwith %}
        {% endfor %}
    </tbody>

{购物车%中的商品为%1}
{%with product=item.product%}
{products%中产品的%s}
{%endfor%}
{{product.name}
.
.
.
{%endwith%}
{%endfor%}
我读过这个

但这对我没用,请帮帮我,我是新手


谢谢

您应该使用反向查找
图像集
。要获取第一个图像,请执行
product.image\u set.first.image.url

<img src="{{ product.image_set.first.image.url }}" alt="" width="auto" height="340"/>
注意:您应该删除此部分:

{% for product in products %} 
{% endfor %}

因为您没有将
产品
变量传递到模板上下文中。

您好,谢谢,但它对我不起作用,没有错误,但没有更改
{% for product in products %} 
{% endfor %}