使用与object_list不同的计数器,而不是for_loop中使用的计数器

时间:2018-09-30 08:35:48

标签: django django-views

我想对模板中的对象进行计数,但是由于我正在使用分页将它们划分为页面,因此我想使用与for循环中使用的对象不同的object_list中的计数器。

这是我的模板

{% 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">&laquo; 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 &raquo;</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”开始。当我单击下一页时,我希望计数器继续显示。

1 个答案:

答案 0 :(得分:0)

您可以枚举obect_list:

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()
    object_list =list(enumerate(object_list))
    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})

然后,对象的结构已更改:product.0将包含计数器,而product.1将包含模型实例:

  {% for product in objects %}
                <tr>
                  <th scope="row">{{product.0}}</th>
                  <td><img class="img img-fluid rounded" width="200" height="136" src="{{ product.1.photo.url }}"></td>
                  <td>{{product.1.product_name}}</td>
                  <td>{{product.1.price}}</td>
                  <td>{{product.1.price_pol}}</td>
                </tr>
            {% endfor %}