django - 有没有办法在 Django 中将分页添加到 html 页面?
问题描述
当我尝试将分页添加到我的主页 html 页面时遇到问题,我在我的代码中尝试了这个但我得到了错误..
我试过这样做:
视图.py
def home_page(request, template='html_file/enterface.html'):
contextt = {
'opratingSystems': OpratingSystems.objects.all(),
'androidgames': AndroidGames.objects.all(),
'androidapk': AndroidApks.objects.all(),
'antivirus': Antivirus.objects.all(),
'pcgames': PCgames.objects.all(),
'pcprogram': PCprogram.objects.all(),
}
app = pcgames.objects.all()
page = request.GET.get('Page', 1) # the_home_page is the name of pages when user go to page 2 etc
paginator = Paginator(app, 6) # 6 that's mean it will show 6 apps in page
try:
pcgame = paginator.page(page)
except PageNotAnInteger:
pcgame = paginator.page(1)
except EmptyPage:
pcgame = paginator.page(paginator.num_pages)
return render(request,template,contextt)
在 HTML 页面中:
<div class="container">
<div class='row'>
{% for pcgame in pcgames %}
<div class='col-xs-12 col-sm-6 col-md-4 website-thumb'>
<a href=" {{ pcgame.page_url }} ">
<img src="{{ pcgame.get_image }}" class='image_control_for_home_page_pc_games' alt=''> </a>
<h3 class="font_control_for_home_page_pc_games_name"><a href=" {{ pcgame.page_url }} ">{{ pcgame.name }}</a></h3>
</div>
{% endfor %}
</div>
在 html 页面的末尾我添加了这个
{% if pcgame.has_previous %}
<a id="border_pagination" class="btn btn-outline-info mb-4" href="?Page=1">First</a>
<a id="border_pagination" class="btn btn-outline-info mb-4" href="?Page={{ pcgame.previous_page_number }}">Previous</a>
{% endif %}
{% for num in pcgame.paginator.page_range %}
{% if pcgame.number == num %}
<a class="btn btn-info mb-4" href="?Page={{ num }}">{{ num }}</a>
{% elif num > pcgame.number|add:'-5' and num < pcgame.number|add:'6' %}
<a class="btn btn-outline-info mb-4" href="?Page={{ num }}">{{ num }}</a>
{% endif %}
{% endfor %}
{% if pcgame.has_next %}
<a id="border_pagination" class="btn btn-outline-info mb-4" href="?Page={{ pcgame.next_page_number }}">Next</a>
<a id="border_pagination" class="btn btn-outline-info mb-4" href="?Page={{ pcgame.paginator.num_pages }}">Last</a>
{% endif %}
但我总是出错,所以请任何人都可以帮助我
多谢 。
解决方案
推荐阅读
- yocto - 用 wic 构建 yocto/poky 图像?
- c# - 使用 SQL Server 身份验证建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误?
- visual-studio - 如何更改 Visual Studio 打开/停靠新窗口的默认窗口?
- php - 使用 PHP 获取 Gmail:imap_open():无法打开流
- google-cloud-functions - GCloud 函数为存储桶对象提供“拒绝访问”
- python - 找到一行时如何从文件中获取下 n 行
- c# - 我可以在循环中有一个断言并获得每个断言的结果,而不仅仅是最后一个断言吗?
- r - 右侧有固定尺寸条的扰乱图
- javascript - 如何从 api 渲染数组数据?
- html - 如何绘制包含 SQL 查询结果的 HTML 表并在 Shell 脚本中发送到邮件