为了账号安全,请及时绑定邮箱和手机立即绑定

使用 ListView django 进行分页

使用 ListView django 进行分页

杨魅力 2022-05-24 17:20:00
我想创建带有搜索和分页的应用程序。分页不适用于 ListView。当我单击“下一步”链接时,我将从起始页http://127.0.0.1:8001/ ---> 移动到 http://127.0.0.1:8001/?city=2但列表的元素没有改变。然后单击“下一步”链接并没有更改网址(http://127.0.0.1:8001/?city=2 --> http://127.0.0.1:8001/?city=2)。你能帮我找出错误吗? 我认为 *.html 文件中的错误,但找不到我的代码: models.pyfrom django.db import modelsclass City(models.Model):    name = models.CharField(max_length=255)    state = models.CharField(max_length=255)    class Meta:      verbose_name_plural = "cities"    def __str__(self):        return self.name网址.py# cities/urls.pyfrom django.urls import pathfrom . import viewsfrom .views import HomePageView, SearchResultsViewurlpatterns = [    path('search/', SearchResultsView.as_view(), name='search_results'),    path('', HomePageView.as_view(), name='home'),    path('city/<int:pk>/', views.city_detail, name='city_detail'),]视图.pyfrom django.shortcuts import renderfrom django.views.generic import TemplateView, ListViewfrom .models import Cityfrom django.db.models import Qfrom django.shortcuts import render, get_object_or_404class HomePageView(ListView):    model = City    template_name = 'cities/home.html'    paginate_by = 3def city_detail(request, pk):    city = get_object_or_404(City, pk=pk)    return render(request, 'cities/city_detail.html', {'city': city})class SearchResultsView(ListView):    model = City    template_name = 'cities/search_results.html'    def get_queryset(self): # new        query = self.request.GET.get('q')        object_list = City.objects.filter(            Q(name__icontains=query) | Q(state__icontains=query)        )        return object_list主页.html<!-- templates/home.html --><h1>HomePage</h1><form action="{% url 'search_results' %}" method="get">  <input name="q" type="text" placeholder="Search..."></form><ul>  {% for city in object_list %}    <li>      <h1><a href="{% url 'city_detail' pk=city.pk %}">{{ city.name }}</a></h1>    </li>  {% endfor %}</ul>
查看完整描述

1 回答

?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

页面查询参数的标准名称是'page'您应该更改查询参数的名称,或者使用该?page=参数呈现模板。


选项 1:更改page_kwarg

您可以通过更改page_kwarg属性 [Django-doc]来更改它:


class HomePageView(ListView):

    model = City

    template_name = 'cities/home.html'

    paginate_by = 3

    page_kwarg = 'city'

选项 2:更改模板

然而,简单地更改模板可能更有意义,以便它page用作参数:


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


查看完整回答
反对 回复 2022-05-24
  • 1 回答
  • 0 关注
  • 165 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号