我按照ytb的视频,一步一步,一切正常,除了图像。我通过django admin上传了链接地址,但它们没有显示。
models.py:
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=255)
price = models.FloatField()
stock = models.IntegerField()
image_url = models.CharField(max_length=2083)
class Offer (models.Model):
code = models.CharField(max_length=10)
description = models.CharField(max_length=255)
discount = models.FloatField()
index.html:
{% extends 'base.html' %}
{% block content%}
<h1>Products</h1>
<div class="row">
{% for product in products %}
<div class="col">
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="{{ product.image_url}}" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">{{ product.name }}</h5>
<p class="card-text">{{ product.price }} RON</p>
<a href="#" class="btn btn-primary">Adauga in cosul tau</a>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock %}
views.py:
from django.http import HttpResponse
from django.shortcuts import render
from .models import Product
def index(request):
products = Product.objects.all()
return render(request, 'index.html', {'products': products})
def new(request):
return HttpResponse('Produse noi')
代码与视频中相同。我试着问chat gpt是否有什么不对劲的地方,但他什么也没发现。我尝试将名称从“image_url”更改为其他任何名称,但不起作用,正如预期的那样。
2条答案
按热度按时间af7jpaap1#
我觉得这会起作用,这就是我改变的:添加
products instead of Product.objects.all()
lqfhib0f2#
我认为你的代码需要“/”befor {{product.image_url}}