Django模板未呈现

时间:2019-03-01 04:36:16

标签: python mysql django django-templates

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

 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>

1 个答案:

答案 0 :(得分:1)

没有看到您的视图,问题似乎出在上下文对象上。上下文是一个字典,您正在传递newdict作为上下文。这意味着newdict的项目将是上下文中的键。要解决此问题,您需要编写如下代码:

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

但是,有一种更好的方式来显示模板。这是一个示例:

views.py

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

accounts / index.html

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