Django应用程序未在模板中显示数据

2024-06-17 15:39:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Django的新手,正在尝试创建我的第一个电子商务网站。我正试图从.JSON文件中获取产品列表,以显示在我的products.html模板中。产品已经就位,如Django管理门户中所示,products在我的views.py文件中定义,我已经完成了loaddata,但没有任何结果。我在下面添加了一些截图和代码来进一步解释

Views.py代码:

from django.shortcuts import render
from .models import Product


def all_products(request):

    products = Product.objects.all()

    context = {
        'products': products,
    }

    return render(request, 'products/products.html', context)

products.html模板:

{% extends "base.html" %}
{% load static %}

{% block page_header %}
<div class="container header-container">
    <div class="row">
        <div class="col"></div>
    </div>
</div>
{% endblock %}

{% block content %}
<div class="container">
    <div class="row">
        <div class="col">
            {{ products }}
        </div>
    </div>
</div>
{% endblock %}

我的文件夹结构是正确的,但我会在这里发布它,以防万一:
enter image description here
任何帮助都将是巨大的,因为我已经被困在这一段时间了。谢谢

型号。py:*

class Product(models.Model):
    category = models.ForeignKey('category', null=True, blank=True, on_delete=models.SET_NULL)
    sku = models.CharField(max_length=254, null=True, blank=True)
    name = models.CharField(max_length=254)
    description = models.TextField()
    price = models.DecimalField(max_digits=6, decimal_places=2)
    rating = models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)
    image_url = models.URLField(null=True, blank=True)
    image = models.ImageField(null=True, blank=True)

    def __str__(self):
        return self.name


Tags: 文件djangopydivtrue产品modelscontainer
2条回答

您需要遍历传递给模板的对象。 比如说,

{% for product in products %}
    <img src={{ product.src }} />
    <h3>{{ product.title }}</h3>
    <p>{{ product.desc }}</p>
{% endfor %}

到目前为止,大部分事情你都做对了。导致问题的一个原因是模板没有迭代所有产品

在views.py中,Product.objects.all()返回一个查询集,您可以想象它是一个包含许多产品实例的列表

因此,当产品(即Product.objects.all())传递给模板时,您需要使用循环来迭代产品内的实例

我假设您的产品模型包含以下三个字段(您可以在稍后我提供的示例中用产品模型中的实际字段替换它们)

  • 名字
  • 描述
  • 数量

现在,在Django模板中,您可以用以下代码替换{{products}}

<table>
    <thead>
    <tr>
        <th scope="col" class="text-nowrap"><span>Product</span></th>
        <th scope="col" class="text-nowrap"><span>Description</span></th>
        <th scope="col" class="text-nowrap"><span>Qty</span></th>
    </tr>
    </thead>
    <tbody>
    {% for product in products %}
        <tr>
            <td><span>{{ product.name }}</span></td><!  Replace the name with your actual field  >
            <td><span>{{ product.desciption }}</span></td><!  Replace the desciption with your actual field  >
            <td><span>{{ product.qty }}</span></td><!  Replace the qty with your actual field  >
        </tr>
    {% endfor %}
    </tbody>
</table>

在上面的例子中,最重要的部分是

{% for product in products %}
    <tr>
        <td><span>{{ product.name }}</span></td>
        <td><span>{{ product.desciption }}</span></td>
        <td><span>{{ product.qty }}</span></td>
    </tr>
{% endfor %}

它类似于下面的python代码

for product in products:
    print(product.name)
    print(product.desciption)
    print(product.qty)

因此,现在模板应该在表上显示产品

相关问题 更多 >