Django分页URL

ff29svar  于 2022-11-26  发布在  Go
关注(0)|答案(2)|浏览(145)

我可以使用下面的代码在http://127.0.0.1:8000/上进行分页:

{% if is_paginated %}
<div class="pagination">
            <span class="page-links">
                {% if page_obj.has_previous %}
                    <a href="/?page={{ page_obj.previous_page_number }}">previous</a>
                {% endif %}
                <span class="page-current">
                    Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
                </span>
                {% if page_obj.has_next %}
                    <a href="/?page={{ page_obj.next_page_number }}">next</a>
                {% endif %}
            </span>
</div>
{% endif %}

views.py

class IndexView(generic.ListView):
    template_name = 'films/index.html'
    paginate_by = 1
    def get_queryset(self):
        return Film.objects.all()'

我想在http://127.0.0.1:8000/film id上对一堆评论分页,但到目前为止还不能这样做。我的hrefs通常像<a href="{% url 'films:add_comment' film_id=film.id %}">Leave a comment</a>,但我不在乎它是如何写的,只要它能工作。
类似上面的我也试过:

class DetailView(generic.DetailView):
    model = Film
    paginate_by = 10
    template_name = 'films/detail.html'

我认为此链接需要更改,以包括电影id <a href="/?page={{ page_obj.next_page_number }}">next</a>,但它没有显示上一个/下一个链接在这个页面上的时刻,就像它是在http://127.0.0.1:8000/上做的
最新消息:
网站首页(作品)

{% if object_list %}

<table id="myTable">
    <tr>
        <th>Title</th>
        <th>Director</th>
        <th>Description</th>
        <th>Released</th>
    </tr>
    {% for film in object_list %}
    <tr>
        <td><a href="{% url 'films:detail' film.id %}">{{ film.title }}</a></td>
        <td>{{ film.director}}</td>
        <td>{{ film.description}}</td>
        <td>{{ film.pub_date}}</td>
    </tr>
    {% endfor %}
</table>

{% if is_paginated %}
<div class="pagination">
            <span class="page-links">
                {% if page_obj.has_previous %}
                    <a href="/?page={{ page_obj.previous_page_number }}">previous</a>
                {% endif %}
                <span class="page-current">
                    Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
                </span>
                {% if page_obj.has_next %}
                    <a href="/?page={{ page_obj.next_page_number }}">next</a>
                {% endif %}
            </span>
</div>
{% endif %}


{% else %}
<p>No films are available.</p>
{% endif %}

detail.html(无法使用)

<table id="myTable">
    <tr>
        <th>Comment</th>
        <th>User</th>
        {% if user.is_authenticated %}
        <th>Update</th>
        <th>Delete</th>
        {% endif %}

       {% for comment in film.comment_set.all %}
    <tr>
        <td>{{ comment.body }}</td>
        <td>{{ comment.user }}</td>
    {% if request.user == comment.user %}
        <td><a href="{% url 'films:update_comment' film_id=film.id comment_id=comment.id %}">Update</a></td>
        <td><a href="{% url 'films:delete_comment' film_id=film.id comment_id=comment.id %}">Delete</a></td>
    {% endif %}
        {% endfor %}
    </tr>

</table>


{% if is_paginated %}
<div class="pagination">
            <span class="page-links">
                {% if page_obj.has_previous %}
                    <a href="?page=={{ page_obj.previous_page_number }}">previous</a>
                {% endif %}
                <span class="page-current">
                    Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
                </span>
                {% if page_obj.has_next %}
                    <a href="?page={{ page_obj.next_page_number }}">next</a>
                {% endif %}
            </span>
</div>
{% endif %}

<h2>Comment</h2>
{% if user.is_authenticated %}
<a href="{% url 'films:add_comment' film_id=film.id %}">Leave a comment</a>
{% else %}
<p>Please log in or register to comment</p>
{% endif %}
m2xkgtsf

m2xkgtsf1#

看看这个tutorial,我用过几次,很有魅力。
您还可以尝试以下操作:

class IndexView(ListView):
    model = Film
    template_name = 'films/detail.html'
    paginate_by = 10

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs) 
        list_films = Film.objects.all()
        paginator = Paginator(list_films, self.paginate_by)

        page = self.request.GET.get('page')

        try:
            film_page = paginator.page(page)
        except PageNotAnInteger:
            film_page = paginator.page(1)
        except EmptyPage:
            film_page = paginator.page(paginator.num_pages)

        context['object_list'] = film_page
        return context
jbose2ul

jbose2ul2#

你可以使用这个,我使用它是因为我在url本身中使用了过滤器,所以所有的url参数都被用来构建下一个或上一个url

import re
from django import template

register = template.Library()
PAGE_NUMBER_REGEX = re.compile(r'(page=[0-9]*[\&]*)')   

@register.simple_tag
def append_page_param(value,pageNumber=None):
'''
remove the param "page" using regex and add the one in the pageNumber
'''
value = re.sub(PAGE_NUMBER_REGEX,'',value) 
if pageNumber:
    if not '?' in value:
        value += f'?page={pageNumber}'
    elif value[-1] != '&':
        value += f'&page={pageNumber}'
    else:
        value += f'page={pageNumber}'
return value

然后,在分页导航中,您可以这样调用它:

{% append_page_param request.get_full_path page_obj.previous_page_number %}

相关问题