gpt4 book ai didi

python - Django 中的分页无法按预期工作

转载 作者:行者123 更新时间:2023-11-28 22:20:40 25 4
gpt4 key购买 nike

我想要所有可用类(class)的分页,这很容易实现。但现在我被困住了,因为我也想为院系分页,这将显示访问的院系的具体类(class)。我有 4 个模型:院系、部门、研究和类(class)。分页也会显示给院系,但问题是如果我尝试转到第二页,它会将我重定向到所有类(class)列表的第二页。或者,如果我更改所有类(class)的页面,然后尝试访问教员,教员中根本不会显示任何类(class)。

def index(request):
course_list = Course.objects.all()
page = request.GET.get('page', 1)
paginator = Paginator(course_list, 1)
try:
courses = paginator.page(page)
except PageNotAnInteger:
courses = paginator.page(1)
except EmptyPage:
courses = paginator.page(paginator.num_pages)
faculty_list = Faculty.objects.all()
page = request.GET.get('page2', 1)
paginator = Paginator(faculty_list, 1)
try:
faculties = paginator.page(page)
except PageNotAnInteger:
faculties = paginator.page(1)
except EmptyPage:
faculties = paginator.page(paginator.num_pages)
context = {'courses': courses,
'faculties': faculties,
'departments': Department.objects.all(),
'studies': StudyProgramme.objects.all(),
'teachers': Teacher.objects.all()
}
return render(request, 'courses/index.html', context)

<div id="crs">
<h3>All courses</h3>
<ul>
{% for course in courses %}
<li><a href={{ course.slug }}>{{ course.name }}</a></li>
{% endfor %}
</ul>
<div class="pagination">
<span class="step-links">
{% if courses.has_previous %}
<a href="?page=1">&laquo; first</a>
<a href="?page={{ courses.previous_page_number }}">{{ courses.previous_page_number }}</a>
{% endif %}
<span class="current">
Page {{ courses.number }} of {{ courses.paginator.num_pages }}
</span>
{% if courses.has_next %}
<a href="?page={{ courses.next_page_number }}">{{ courses.next_page_number }}</a>
<a href="?page={{ courses.paginator.num_pages }}">last &raquo;</a>

{% endif %}
</span>
</div>
</div>
{% for faculty in faculties %}
<div id="fac_{{ faculty.pk }}_tab" style="display:none;">
<h3> {{ faculty.name }} Courses</h3>
<ul>
{% for department in faculty.department_set.all %}
{% for study in studies %}
{% if study.department == department %}
{% for course in courses %}
{% if course.study_programme == study %}
<li>
<a class="first" href={{ course.slug }}>{{ course.name }}</a>
</li>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
</ul>
<div class="pagination">
<span class="step-links">
{% if faculties.has_previous %}
<a href="?page2=1">&laquo; first</a>
<a href="?page2={{ faculties.previous_page_number }}">{{ faculties.previous_page_number }}</a>
{% endif %}
<span class="current">
Page {{ faculties.number }} of {{ faculties.paginator.num_pages }}
</span>
{% if faculties.has_next %}
<a href="?page2={{ faculties.next_page_number }}">{{ faculties.next_page_number }}</a>
<a href="?page2={{ faculties.paginator.num_pages }}">last &raquo;</a>
{% endif %}
</span>
</div>
</div>
{% endfor %}

最佳答案

这不是 Django 问题,您的 View 肯定会处理一个分页参数 page ,它将指向类(class),因此如果您更改每个模型分页的 get 参数,它将起作用。

例如:

## views.py
page = request.GET.get('page')
## yourtemplate.html
<a href="?page={{ yourcontext }}">yourtext</a>
localhost:8000/MYVIEW/?page=2 # Goes to courses

## views.py
page = request.GET.get('faculties')
## yourtemplate.html
<a href="?faculties={{ yourcontext }}">yourtext</a>
##
localhost:8000/MYVIEW/?faculties=2 # Goes to faculties

等..

编辑:

{% for faculty in faculties %} 将其更改为 {% for faculty in faculties2 %} 并从您的上下文中删除 faculties,因为您在重复自己,因为 faculties2 和 faculties 持有相同的查询集。

关于python - Django 中的分页无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48772298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com