我正在使用django作为网站的后端
将有10个qwestions 我想对它们进行分页(给出一个qwestion的ans,然后转到下一个,依此类推),最后给出基于所有给出的答案计算的分数
关于如何操作的任何提示?
我发现类似的东西,但不知道如何在Django中实现
答案 0 :(得分:1)
您是否看过Django Pagination文档?
https://docs.djangoproject.com/en/2.1/topics/pagination/
从该页面
您的视图:
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.shortcuts import render
def listing(request):
contact_list = Contacts.objects.all()
paginator = Paginator(contact_list, 25) # 25 is the items per page, in Your case would be 1
page = request.GET.get('page')
contacts = paginator.get_page(page)
return render(request, 'list.html', {'contacts': contacts})
您的模板:
{% for contact in contacts %}
{# Each "contact" is a Contact model object. #}
{{ contact.full_name|upper }}<br>
...
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if contacts.has_previous %}
<a href="?page=1">« first</a>
<a href="?page={{ contacts.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
</span>
{% if contacts.has_next %}
<a href="?page={{ contacts.next_page_number }}">next</a>
<a href="?page={{ contacts.paginator.num_pages }}">last »</a>
{% endif %}
</span>
</div>
要转到某个页面,您应该添加到网址?page=1
,其中1是页面编号。