我不确定我错在哪里。我正在使用Django/HTML/Foundations创建一个web商店,但我无法将数据库中的产品显示在网页上。我知道它们在数据库中,因为当我转到管理页面时,它们就会出现。
这段HTML代码如下:
{% for products in Product %}
<div class="column">
<h5>Title: {{products.product_name}}</h5>
<h5>Type: {{products.product_type}}</h5>
<h5>Price: {{products.sales_price}}</h5>
<img class="thumbnail" src="http://placehold.it/550x550">
</div>
{% endfor %}
以下是模型:
class Product(models.Model):
product_name = models.CharField(max_length=255)
product_type = models.CharField(max_length=100)
sales_price = models.CharField(max_length=10)g
def __str__(self):
return self.product_name + " " + self.product_type + " " + self.sales_price
我的产品views.py页面中唯一的内容是:(这可能就是我的问题所在)
def products(request):
return render(request,"products.html")
我是django和python的新手。有人能解释一下发生了什么事吗?谢谢
发布于 2017-11-22 04:12:55
您的视图需要使用context
参数向模板提供products
信息。See the documentation for render()。
views.py:
def products(request):
context = {'products':Product.objects.all()}
return render(request,"products.html",context)
products.html:
{% for product in products %}
<div class="column">
<h5>Title: {{product.product_name}}</h5>
<h5>Type: {{product.product_type}}</h5>
<h5>Price: {{product.sales_price}}</h5>
<img class="thumbnail" src="http://placehold.it/550x550">
</div>
{% endfor %}
发布于 2017-11-22 04:13:06
你有没有试过把for循环从
{% for products in Product %}
至
{% for product in products %}
因为您希望在每次处理循环时显示产品组中的单个产品。
https://stackoverflow.com/questions/47421708
复制相似问题