欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用python對(duì)mysql表做全局模糊搜索并分頁(yè)實(shí)例

 更新時(shí)間:2020年07月12日 13:36:19   作者:Monster_ixx  
這篇文章主要介紹了利用python對(duì)mysql表做全局模糊搜索并分頁(yè)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

在寫(xiě)django項(xiàng)目的時(shí)候,有的數(shù)據(jù)沒(méi)有使用模型管理(數(shù)據(jù)表是動(dòng)態(tài)添加的),所以要直接使用mysql。前端請(qǐng)求數(shù)據(jù)的時(shí)候可能會(huì)指定這幾個(gè)參數(shù):要請(qǐng)求的頁(yè)號(hào),頁(yè)大小,以及檢索條件。

"""
	tableName: 表名
	pageNum: 請(qǐng)求的頁(yè)的編號(hào)
	pageSize: 每一頁(yè)的大小
	searchInfo: 需要全局查詢(xún)的信息
"""
def getMysqlData(tableName, pageNum, pageSize, searchInfo):
	# 使用MySQLdb獲取的mysql游標(biāo)
  cursor = getCursor()
  # 用以獲取列標(biāo)題
  colSql = 'select * from {} limit 1'.format(tableName)
  cursor.execute(colSql)
  columns = [col[0] for col in cursor.description]
  # 轉(zhuǎn)化查詢(xún)信息為sql
  searchCondition = ','.join(columns)
  searchInfo = "WHERE CONCAT({}) like '%{}%'".format(searchCondition, searchInfo)
  # 用以獲取總數(shù)
  totalSql = "select count(*) from {} {};".format(tableName, searchInfo)
  cursor.execute(totalSql)
  total = cursor.fetchone()[0]
  # 用以獲取具體數(shù)據(jù)
  limit1 = (pageNum - 1) * pageSize
  limit2 = pageSize
  dataSql = "select * from {} {} limit {},{};".format(tableName, searchInfo, limit1, limit2)
  cursor.execute(dataSql)
  data = [
    dict(zip(columns, row)) 
    for row in cursor.fetchall()
  ]
  return (total, columns, data)

"""
	total: 符合條件的數(shù)據(jù)總數(shù)
	columns: 字段名列表 
	['字段名1', '字段名2', ...]
	data: 數(shù)據(jù)對(duì)象列表 
	[{'字段名1': 數(shù)據(jù)1,'字段名2':數(shù)據(jù)1, ...},{'字段名1': 數(shù)據(jù)2, '字段名2': 數(shù)據(jù)2, ...}, ...]
"""

補(bǔ)充知識(shí):django 分頁(yè)查詢(xún)搜索--傳遞查詢(xún)參數(shù),翻頁(yè)時(shí)帶上查詢(xún)參數(shù)

django在分頁(yè)查詢(xún)的時(shí)候,翻頁(yè)時(shí),v層要傳遞查詢(xún)參數(shù),相應(yīng)的html翻頁(yè)連接也要帶上查詢(xún)參數(shù)

直接上代碼

view:

@login_required
def search_name(request):
 
 
  username = request.session.get('user')
  search_name = request.GET.get('name')
  if search_name == None:
    search_name = request.GET.get('name')
 
 
  event_list = Event.objects.filter(name__contains=search_name)
 
  paginator = Paginator(event_list, 2)
  page = request.GET.get('page')
  try:
    contacts = paginator.page(page)
 
  except PageNotAnInteger:
    # 如果page不是整數(shù),取第一頁(yè)面數(shù)據(jù)
    contacts = paginator.page(1)
  except EmptyPage:
    # 如果page不在范圍內(nèi),則返回最后一頁(yè)數(shù)據(jù)
    contacts = paginator.page(paginator.num_pages)
 
  return render(request,'event_manage.html',{'user':username,'events':contacts,'name':search_name})

html:

 <!--列表分頁(yè)器-->
  <div class="pagination">
  <span class="step-links">
    {% if events.has_previous %}
      <a href="?page={{ events.previous_page_number }}&&name={{ name }}" rel="external nofollow" >previous</a>
    {% endif %}
    <span class="current">
      Page {{ events.number }} of {{ events.paginator.num_pages }}
 
    </span>
    {% if events.has_next %}
      <a href="?page={{ events.next_page_number }}&name={{ name }}" rel="external nofollow" >next</a>
    {% endif %}
 
  </span>
  </div>
  {% include 'include/pager.html' %}

以上這篇利用python對(duì)mysql表做全局模糊搜索并分頁(yè)實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論