Я хочу посчитать объекты в моем шаблоне, но, поскольку я использую разбиение на страницы для разделения их на страницы, я хочу использовать счетчик из другого object_list, чем тот, который используется в цикле for
.
Это мой шаблон:
{% extends 'base.html' %}
{% block content %}
{% if user.is_superuser %}
<div class="row">
<div class="col-sm-4 offset-sm-4">
<form class="form-inline md-form form-sm" method="GET" action="">
<i class="fa fa-search" aria-hidden="true"></i>
<input class="form-control form-control-sm ml-3 w-75" type="text" placeholder="搜索货品 Znajdź część" name="q" value="{{ request.GET.q }}"/>
</form>
</div>
</div>
<br/>
<hr/>
<div class='row'>
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Image</th>
<th scope="col">Product name</th>
<th scope="col">Price (cost)</th>
<th scope="col">Price (PL)</th>
</tr>
</thead>
<tbody>
{% for product in objects %}
<tr>
<th scope="row">{{forloop.counter}}</th>
<td><img class="img img-fluid rounded" width="200" height="136" src="{{ product.photo.url }}"></td>
<td>{{product.product_name}}</td>
<td>{{product.price}}</td>
<td>{{product.price_pol}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="col-sm-4 offset-sm-4" align="center">
<div class="pagination">
<span class="step-links">
{% if objects.has_previous %}
<a href="?page=1">« first</a>
<a href="?page={{ objects.previous_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">previous</a>
{% endif %}
<span class="current">
Page {{ objects.number }} of {{ objects.paginator.num_pages }}.
</span>
{% if objects.has_next %}
<a href="?page={{ objects.next_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">next</a>
<a href="?page={{ objects.paginator.num_pages }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">last »</a>
{% endif %}
</span>
</div>
</div>
{% endif %}
{% endblock content %}
Мои views.py:
Здесь я хотел бы использовать счетчик из object_list
, а не object
(обратите внимание на контекст в productview
)
@login_required
def productview(request):
object_list = Product.objects.all().order_by('-date')
query = request.GET.get('q')
if query:
object_list = object_list.filter(
Q(product_name__icontains=query)|
Q(date__icontains=query)
).distinct()
paginator = Paginator(object_list, 5)
page = request.GET.get('page')
objects = paginator.get_page(page)
return render(request, 'products_list.html', {'objects': objects, 'object_list':object_list})
Я хотел использовать вложенный цикл, но это не сработало.Есть ли другой способ, о котором я не знаю?Если я оставляю это так каждый раз, когда нажимаю nextpage
, счетчик будет начинаться с «1».Я хотел бы, чтобы счетчик продолжался, когда я нажимаю на следующую страницу ...