我正在尝试将Django分页与mysql数据库连接,但是我遇到了一些问题
问题
view.py
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
def displayLogs(request): 从django.http导入HttpResponseRedirect 从django.shortcuts导入renderimport mysql.connector
con = mysql.connector.connect (host="localhost", user = "root", passwd ="12345", db = "database")
cur=con.cursor()
sql = "SELECT * FROM Table "
cur.execute(sql)
user_list=cur.fetchall()
searchlen=len(user_list)
paginator = Paginator(user_list, 100)
page = request.GET.get('page')
users = paginator.get_page(page)
return render(request,'displayLogs.html',{'users': users,'searchlen':searchlen})
template.html
<table cellpadding="0" cellspacing="0" border="0" class="table table table-striped table-hover table-bordered">
<thead>
<tr>
<th ><center><b>ActivityDate</center></th>
<th><center><b>Activity</center></th>
<th><center><b>ActorUserPrinicipalName</center></th>
<th><center><b>Target1Name</center></th>
</tr>
</thead>
<tbody>
<tr class="gradeX">
{% for item in users %}
<td>{{item.0}}</td>
<td>{{item.1}}</td>
<td>{{item.6}}</td>
<td class="center">{{item.10}}</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<th ><center><b>ActivityDate</center></th>
<th><center><b>Activity</center></th>
<th><center><b>ActorUserPrinicipalName</center></th>
<th><center><b>Target1Name</center></th>
</tr>
</tfoot>
<div class="pagination" style="border-style:inset;border-width:5px;padding-bottom: 5px">
{% if users.has_previous %}
<a class="pagination-action" href="?page=1"> <i class="fas fa-angle-double-left"></i></a>
<a class="pagination-action" href="?page={{ users.previous_page_number}}"> <i class="fas fa-angle-left"></i></a>
{% endif %}
{% for num in users.paginator.page_range %}
{% if users.number == num %}
<span class="pagination-number pagination-current"><strong>{{ num }}</strong></span>
{% elif num > users.number|add:'-3' and num < users.number|add:'3' %}
<a class="pagination-number" href="?page={{ num }}">{{num}}</a>
{% endif %}
{% endfor %}
{% if users.has_next %}
<a class="pagination-action" href="?page={{ users.next_page_number }}"><i class="fas fa-angle-right"></i></a>
<a class="pagination-action" href="?page={{ users.paginator.num_pages }}"> <i class="fas fa-angle-double-right"> </i></a>
{% endif %}
</div>