我想显示从查询搜索返回的对象数。我试过了
{{ p.count }} and {{ Post.count }}
这是我的post_list.html。我已阅读其他使用这些方法的帖子,但它们对我不起作用。我知道我错过了什么。
{% extends 'posts/base.html' %}
{% block content %}
<div class="col-sm-6 col-sm-offset-3">
<h1>{{ title }}</h1>
<form method="get" action=" ">
<input type="text" name="q" placeholder="search" value="{{ request.GET.q }}"/>
<input type="submit" value=" search"/>
</form>
<a href="{% url 'posts:create' %}">create</a>
{% for p in queryset %}
<div class="row">
<div class="col-sm-12 "> <!-- i like col-sm-6 -->
<div class="thumbnail">
{% if p.image %}
<img src='{{ p.image.url }}' class="img-responsive" />
{% endif %}
<!--<img src="..." alt="...">-->
<div class="caption">
{% if p.draft %} <h3>Staff Only: Draft</h3> {% if p.publish > today %}<h3>Staff Only: Future Post</h3> {% endif %}
{% endif %}
<h3><a href='{{ p.get_absolute_url}}'>{{p.title}}</a> <small>{{p.publish | timesince }} </small> </h3>
{% if p.user.get_full_name %}<p>Author: {{ p.user.get_full_name }}</p>{% endif %}
<p>{{p.content | truncatechars:30}}</p>
<p><a href="{{ p.get_absolute_url}}" class="btn btn-primary" role="button">View</a>
{% if user.is_authenticated %}
<a href='{% url "posts:update" p.slug %}' class="btn btn-default" role="button">edit</a>
<a href='{% url "posts:delete" p.id %}' class="btn btn-danger" role="button">delete</a>
{% endif %}
</p>
</div>
</div>
</div>
<hr>
</div>
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if queryset.has_previous %}
<a href="?{{ page_request_var }}={{ queryset.previous_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">previous</a>
{% endif %}
<span class="current">
Page {{ queryset.number }} of {{ queryset.paginator.num_pages }}.
</span>
{% if queryset.has_next %}
<a href="?{{ page_request_var }}={{ queryset.next_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">next</a>
{% endif %}
</span>
</div>
</div>
{% endblock content %}
任何帮助将不胜感激。感谢
答案 0 :(得分:0)
答案 1 :(得分:0)
您无法对单个对象count
执行p
,因为无法计算。只有查询集具有count
方法,所以:
{{ queryset.count }}