Вопрос

will try and keep it simple! :)

I've setup pagination within my django app. I'm manaing to loop through the pages i've setup in my pagination. There are 12 pages with 10 news posts per page. I'm able to click next and the page number I wish to view which all works fine.

However the data within the post from the backend is not showing and I can't see why. I think it's because i'm not accessing the model properly from thr template. Anyway here my code:

Views.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))

Pagination_template:

<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}}'>&#60;</a>{% else %}<span>&#60;</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}}'>&#62;</a>{% else %}<span>&#62;</span>{%endif%}</li>
        </ul>
</div>

News post content:

 <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>

I'm sure it's the - {% for object in paginator.page_range %} line that's doing it as currently it's just looping through the number of posts but not the content. I'm not sure how to access the data.

Any help would be much appreciate. Pretty new to Python and Django and i'm pretty impressed with what I've done so far! :)

Это было полезно?

Решение

You are passing this_page.object_list and paginator as 'paginator' in the context. So paginator is not actually a oblect_list in the template.

But you don't need to pass that in template, rather you can make use of this_page, so your template can be updated to

<ul class="article-list">
    {% for object in this_page.object_list %}
       ... your rendering code ...
    {%endfor%}
</ul>

For django 1.4 and above you can directly do

{% for object in this_page %}
    ...
{%endfor%}

Reference : Using Paginator in a view

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top