首页 > 解决方案 > 有没有办法在 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 %}

但我总是出错,所以请任何人都可以帮助我

多谢 。

标签: djangodjango-rest-frameworkdjango-viewsdjango-templates

解决方案


推荐阅读