Python Django for loop未在网页上显示我的项目
我在我的网页上有一个表,我想迭代产品信息,直到我尝试添加一个分页器。服务器运行正常,网站的其余部分正常运行 这是我的档案: shop/products.htmlPython Django for loop未在网页上显示我的项目,python,django,Python,Django,我在我的网页上有一个表,我想迭代产品信息,直到我尝试添加一个分页器。服务器运行正常,网站的其余部分正常运行 这是我的档案: shop/products.html {% extends 'base.html' %} {% block content %} <h1>On sale here</h1> <div class="col-sm-3"> <h4>Categories</h4> <ul class="list-
{% extends 'base.html' %}
{% block content %}
<h1>On sale here</h1>
<div class="col-sm-3">
<h4>Categories</h4>
<ul class="list-group">
<a href="{% url 'products' %}" class="list-group-item"> All Categories </a>
{% for c in countcat %}
<a href="{{ c.get_absolute_url }}" class="list-group-item catheight">{{c.name}}
<span class="badge">{{c.num_products}}</span>
</a>
{% endfor %}
</ul>
</div>
<div class="col-sm-9">
<h4>Note that all products are second hand, unless otherwise stated.</h4>
<form class="form-inline my-2 my-lg-0" action="{% url 'search_result' %}" method="get">
{% csrf_token %}
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" name="q">
<button class="glyphicon glyphicon-search h-100" type="submit"></button>
</form>
<table class="table table-bordered table-hover table-condensed">
<thead>
<!-- The header row-->
<tr>
<th>ID</th>
<th>Name</th>
<th>Image</th>
<th>Category</th>
<th>Description</th>
<th>Stock</th>
<th>Price</th>
<th>Buy</th>
</tr>
</thead>
<tbody>
<!-- Product row(s) -->
{% for product in products %}
<tr>
<td>{{product.id}}</td>
<td>{{product.name}}</td>
{% if product.image %}
<td><img src="{{product.image.url}}"></td>
{% endif %}
<td>{{product.category}}</td>
<td>{{product.description}}</td>
<td>{{product.stock}}</td>
<td>€{{product.price}}</td>
{% if product.stock > 0 %}
<td><a href="{% url 'add_cart' product.id %}" class="btn btn-default btn-xs"><span
class="glyphicon glyphicon-shopping-cart"></span></a></td>
{% else %}
<td><a href="" class="btn btn-default btn-xs"><span
class="glyphicon glyphicon-warning-sign red"></span></a></td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
<hr>
<div class="text-center">
{% for pg in products.paginator.page_range %}
<a href="?page={{pg}}" class="btn btn-light btn-sm
{% if products.number == pg %}
active
{% endif %}">{{pg}}</a>
{% endfor %}
</div>
</div>
{% endblock content %}
shop/url.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.product_list,
name='products'),
path('<int:category_id>/', views.product_list,
name='product_list_by_category'),
]
我有点像django noob,因此非常感谢您的帮助,谢谢:)您在这里向Paginator传递字符串:
paginator = Paginator('products', 3)
您应该传递一个对象列表:
paginator = Paginator(products, 3)
paginator = Paginator('products', 3)
paginator = Paginator(products, 3)