RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1607215
Accepted
MoonInBlack
MoonInBlack
Asked:2025-02-19 13:47:50 +0000 UTC2025-02-19 13:47:50 +0000 UTC 2025-02-19 13:47:50 +0000 UTC

ListView 分页重定向到主页

  • 772

我根据 Listview 类进行分页。当我点击任何按钮 - “下一页”,“1 2 3”时,我会被重定向到主页“主页”。也许有人可以解释为什么会发生这种情况?我不是要求你为我编写代码,只是解释一下这里发生了什么。

视图.py

class AllPost(DataMixin, ListView):
template_name = 'main_package/all_post.html'
context_object_name = 'all_post'
title_page = 'ПЛАТФОРМА44.РУ - Все статьи'
paginate_by = 9

def get_queryset(self):
    return Post.published.all().select_related('cat')

那也不起作用。

class AllPost(DataMixin, ListView):
template_name = 'main_package/all_post.html'
context_object_name = 'all_post'
title_page = 'ПЛАТФОРМА44.РУ - Все статьи'
contact_list = Post.published.all()
paginator = Paginator(contact_list, 4)
paginate_by = 9

def get_queryset(self):
    return Post.published.all()

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['page_obj'] = self.paginator.page(self.request.GET.get('page', 1))
    return context

它也无法通过该函数起作用。

contact_list = Post.published.all()
paginator = Paginator(contact_list, 3)
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
return render(request, 'main_package/all_post.html',
              {'title': 'Все статьи', 'page_obj': page_obj})

全部帖子.html

    <a href="?page={{ page_obj.next_page_number }}" class="button" title="Следующая страница"
       {% if not page_obj.has_next %} style="display:none;" {% endif %}>Следующая страница <i
            class="fa fa-angle-double-right"></i></a>
    <div class="pagination">
        {% if page_obj.has_previous %}
        <a href="?page={{ page_obj.previous_page_number }}" class="pagination-left" title="Предыдущая страница">&laquo;</a>
        {% endif %}
        {% for p in page_obj.paginator.page_range %}
        {% if page_obj.number == p %}
        <span class="active">{{ p }}</span>
        {% else %}
        <a href="?page={{ p }}" title="Перейти на страницу {{ p }}">{{ p }}</a>
        {% endif %}
        {% endfor %}
        {% if page_obj.has_next %}
        <a href="?page={{ page_obj.next_page_number }}" class="pagination-right"
           title="Следующая страница">&raquo;</a>
        {% endif %}
    </div>
</section>

而且它也不起作用。

{% if page_obj.has_other_pages %}
<nav class="list-pages">
<ul>
    {% if page_obj.has_previous %}
    <li class="page-num">
        <a href="?page={{ page_obj.previous_page_number }}">&lt;</a>
    </li>
    {% endif %}
    {% for p in paginator.page_range %}
    {% if page_obj.number == p %}
    <li class="page-num page-num-selected">{{ p }}</li>
    {% elif p >= page_obj.number|add:-2 and p <= page_obj.number|add:2 %}
    <li class="page-num">
        <a href="?page={{ p }}">{{ p }}</a>
    </li>
    {% endif %}
    {% endfor %}
    {% if page_obj.has_next %}
    <li class="page-num">
        <a href="?page={{ page_obj.next_page_number }}">&gt;</a>
    </li>
    {% endif %}
 </ul>
 </nav>
{% endif %}

urls.py

urlpatterns = [
path('', views.PlatformaHome.as_view(), name='home'),
path('category/<slug:cat_slug>/', views.PlatformaCategory.as_view(), name='category'),
path('post/<slug:post_slug>/', views.ShowPost.as_view(), name='post'),
path('addpage/', views.AddPage.as_view(), name='addpage'),
path('edit/<slug:slug>/', views.UpdatePage.as_view(), name='edit_page'),

path('all_post/', views.AllPost.as_view(), name='all_post'),
python
  • 2 2 个回答
  • 43 Views

2 个回答

  • Voted
  1. Best Answer
    Max Watson
    2025-02-20T16:51:03Z2025-02-20T16:51:03Z

    从题目可以看出,作者并没有要求写代码,而是要求解释原因。我认为,应检查和使用以下事项:

    1. 您是否要访问该地址:http://127.0.0.1: 8000/all_post/ ,并且不要all_post没有слэша。

    2. 在模板中,使用标准变量is_paginated、page_obj、paginator。不要Paginator在不需要的地方手动复制。

    3. 就我个人而言,我遇到过这样的情况,在我的项目中有一个重定向正在干扰类似的事情;request.GET.get('page')请检查你的项目中是否有类似的事情。通常可以从中继承一些东西,base.html也许那里有一些重定向。

    • 0
  2. MoonInBlack
    2025-02-22T03:09:46Z2025-02-22T03:09:46Z

    我要向Max Watson表示最深切的谢意,感谢他拯救了我的神经系统。确实,问题出在base.html中,只有一个斜线。

    <meta charset="utf-8">
        <base href="/">
    

    基础.html

    谢谢。

    • 0

相关问题

  • 是否可以以某种方式自定义 QTabWidget?

  • telebot.anihelper.ApiException 错误

  • Python。检查一个数字是否是 3 的幂。输出 无

  • 解析多个响应

  • 交换两个数组的元素,以便它们的新内容也反转

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5