会尽量保持简单!:)
我已经在我的 django 应用程序中设置了分页。我正在循环浏览我在分页中设置的页面。有12页,每页10条新闻。我可以单击下一步和我希望查看的页码,这一切正常。
但是来自后端的帖子中的数据没有显示,我不明白为什么。我认为这是因为我没有从 thr 模板正确访问模型。无论如何,这里是我的代码:
视图.py:
def press_list(request, template_name='pressbox/object_list.html', extra_context={}):
category = PressCategory.objects.get()
news_items = PressItem.objects.filter(is_active=True)
paginator = Paginator(news_items, 10)
page = request.GET.get('page', 1)
this_page = paginator.page(page)
page = int(page)
try:
this_page = paginator.page(page)
except(EmptyPage, InvalidPage):
raise Http404
extra_context = {
'category': category,
'paginator': this_page.object_list,
'paginator': paginator,
"current_page_pag": page,
'this_page': this_page,
}
return render_to_response(template_name, extra_context, RequestContext(request))
分页模板:
<div class="pag_wrap">
<ul class='news_paginator'>
<li class='prev'>{% if this_page.has_previous %}<a href='{{current_url}}?page={{this_page.previous_page_number}}'><</a>{% else %}<span><</span>{%endif%}</li>
{% for page in paginator.page_range %}
<li class='{% if forloop.last %}last {%endif%}{%ifequal current_page_pag page %}active {%endifequal%}'><a href='{{current_url}}?page={{page}}'>{{page}}</a></li>
{% endfor %}
<li class='next'>{% if this_page.has_next %}<a href='{{current_url}}?page={{this_page.next_page_number}}'>></a>{% else %}<span>></span>{%endif%}</li>
</ul>
</div>
新闻发布内容:
<ul class="article-list">
{% for object in paginator.page_range %}
<li class="{{ object.category.slug }}">
<a href="{{ object.get_absolute_url }}">
<img class="news_thumbnail" src="/media/{{object.thumbnail}}" alt="thumbnail" />
<strong class="article-title">{{ object.title }}</strong>
<span class="article-date">{{ object.published_on|date:"M d, Y" }}</span>
{{ object.short_description }}
{% comment %} <span class="article-category">{{ object.category.slug }}</span> {% endcomment %}
</a>
</li>
{% endfor %}
</ul>
我确定是 - {% for object in paginator.page_range %} 行正在这样做,因为目前它只是循环通过帖子数量而不是内容。我不确定如何访问数据。
任何帮助将不胜感激。对 Python 和 Django 来说还很陌生,我对迄今为止所做的事情印象深刻!:)