使用mysql数据库的Django分页无法正常工作

时间:2019-03-13 13:03:05

标签: python mysql django pagination django-pagination

我正在尝试将Django分页与mysql数据库连接,但是我遇到了一些问题

  

问题

  1. 当数据较少时工作正常。
  2. 但是当数据为百万时,即使我一次只获取100行,也需要2-3分钟才能将数据加载到html页面中。
  3. 每次更改页面时,需要花费2-3分钟的时间将数据加载到html页面中
  

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>

0 个答案:

没有答案