python - Django 模板未呈现

标签 python mysql django django-templates

我正在尝试使用 Django 模板从 MySQL 数据库加载对象列表。我可以使用 Product.objects.values() 从数据库加载对象并将其转换为字典,然后将该字典作为上下文传递给渲染函数。但是当我加载页面时,它不会迭代字典。有人有什么想法吗?

 def product(request):
    print(Product.objects.values())

    d = Product.objects.values()

    newdict = {}

    for entry in d:
        name = entry.pop('name')  # remove and return the name field to use as a key
        newdict[name] = entry

    print(newdict)
    return render(request, 'product.html', newdict)




 <div class="col-12 col-lg-6 add_to_cart_block">

            {% for item in newdict.items %}
            {{newdict|length}}
            <div class="card bg-light mb-3">
                <div class="card-body">
                    <p class="price">{{item}}</p>
                    <p class="price_discounted">149.90 $</p>
                    <form method="get" action="cart.html">
                        <div class="form-group">
                            <label for="colors">Color</label>
                            <select class="custom-select" id="colors">
                                <option selected>Select</option>
                                <option value="1">Blue</option>
                                <option value="2">Red</option>
                                <option value="3">Green</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>Quantity :</label>
                            <div class="input-group mb-3">
                                <div class="input-group-prepend">
                                    <button type="button" class="quantity-left-minus btn btn-danger btn-number"  data-type="minus" data-field="">
                                        <i class="fa fa-minus"></i>
                                    </button>
                                </div>
                                <input type="text" class="form-control"  id="quantity" name="quantity" min="1" max="100" value="1">
                                <div class="input-group-append">
                                    <button type="button" class="quantity-right-plus btn btn-success btn-number" data-type="plus" data-field="">
                                        <i class="fa fa-plus"></i>
                                    </button>
                                </div>
                            </div>
                        </div>
                        <a href="cart.html" class="btn btn-success btn-lg btn-block text-uppercase">
                            <i class="fa fa-shopping-cart"></i> Add To Cart
                        </a>
                    </form>
                    <div class="product_rassurance">
                        <ul class="list-inline">
                            <li class="list-inline-item"><i class="fa fa-truck fa-2x"></i><br/>Fast delivery</li>
                            <li class="list-inline-item"><i class="fa fa-credit-card fa-2x"></i><br/>Secure payment</li>
                            <li class="list-inline-item"><i class="fa fa-phone fa-2x"></i><br/>+33 1 22 54 65 60</li>
                        </ul>
                    </div>
                    <div class="reviews_product p-3 mb-2 ">
                        3 reviews
                        <i class="fa fa-star"></i>
                        <i class="fa fa-star"></i>
                        <i class="fa fa-star"></i>
                        <i class="fa fa-star"></i>
                        <i class="fa fa-star"></i>
                        (4/5)
                        <a class="pull-right" href="#reviews">View all reviews</a>
                    </div>
                    <div class="datasheet p-3 mb-2 bg-info text-white">
                        <a href="" class="text-white"><i class="fa fa-file-text"></i> Download DataSheet</a>
                    </div>
                </div>
            </div>
        </div>

        {% endfor %}
    </div>

最佳答案

没有看到您的观点,看来问题出在上下文对象上。上下文是一个字典,并且您将 newdict 作为上下文传递。这意味着 newdict 的项目将成为上下文中的键。要解决此问题,您需要编写如下行:

return render(request, 'product.html', {'newdict': newdict})

但是,有更好的方式在模板中显示。这是一个例子:

View .py

@login_required
def account_index():
    accounts = Account.objects.all()
    context = {'accounts': accounts}
    return render(request, 'accounts/index.html', context)

账户/index.html

<tbody>
      {% for account in accounts %}
          <tr>
              <td>{{ account.name }}</td>
              <td>{{ account.number }}</td>
              <td>{{ account.route }}</td>
          </tr>
      {% endfor %}
</tbody>

关于python - Django 模板未呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54938009/

相关文章:

python - 用时间戳分割字符串

python - 如何按日期范围更新打印行

MySQL 可选约束

python - 由于不在 TTY 中工作,无法在 Django 中创建 super 用户

python - 使用 python MySQLdb 解析 MySQL 数据库以提取主题标签

python - 您如何形成数据以在 scikit 学习决策树中制作一个包含 n 个特征和 n 个样本的数组?

php - 外交叉连接?

mysql - 使用更新和插入触发器在其他关系中提供计数列

javascript - 使用javascript加载表单提交内容标签内?

python - Django ModelForm 无法使用自定义日期格式正确验证