Django分页URL

时间:2018-01-28 01:32:36

标签: django pagination

我可以使用下面的代码在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/上所做的那样

更新:

index.html(工作)

{% 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 %}

1 个答案:

答案 0 :(得分:0)

请查看此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