2015-11-05 64 views
1

我通過下面的代碼得到蟒蛇的項目清單:分頁項目在python燒瓶列表

from flask_paginate import Pagination 
@app.route('/retrieve_data') 
def retrieve(): 
    PER_PAGE=5 
    connection = MongoClient() 
    db=connection.rheoML 
    fs = gridfs.GridFS(db) 
    search = False 
    q = request.args.get('q') 
    if q: 
     search = True 
    try: 
     page = int(request.args.get('page', 2)) 
    except ValueError: 
     page = 1 
    List=fs.list() 
    pagination = Pagination(page=page,per_page=PER_PAGE, total=len(List), search=search, record_name='List') 
    return render_template("retrieveFile.html",List=List,fs=fs,form="submitIt",pagination=pagination,) 

我下面在https://pythonhosted.org/Flask-paginate/ 還有教程我在我的retrieveFile.html以下至適應分頁:

<form id="submitIt" name="submitIt" action="/GetFile" method="Post" onsubmit="return validate(this)"> 
{{ pagination.info }} 
<table> 
<tr> 
    <th>Select</th><th>Filename</th><th>Operator</th> 
</tr> 
{% for file in List %} 
<tr> 
<td> 
<input type="checkbox" name="FileName" value={{file.strip('u').strip("'")}}><br> 
</td> 
<td> 
<name="FileName1" id="file" value={{file.strip('u').strip("'")}}>{{file.strip('u').strip("'")}}<br> 
</td> 
<td> 
{{fs.get_last_version(file).Operator}}<br> 
</td> 
</tr> 
{% endfor %} 
</table> 
{{ pagination.links }} 
<input type="submit" value="Download"> 
</form> 

我在風格標籤添加此

<style> 
.pagination-page-info { 
    padding: .6em; 
    padding-left: 0; 
    width: 40em; 
    margin: .5em; 
    margin-left: 0; 
    font-size: 12px; 
} 
.pagination-page-info b { 
    color: black; 
    background: #6aa6ed; 
    padding-left: 2px; 
    padding: .1em .25em; 
    font-size: 150%; 
} 
</style> 

問題是頁面顯示時,頁面沒有問題,但所有的項目都顯示在一個頁面中。請幫我,我仍然在分頁初學者,還是想工作的事情了

回答

1

得到它整理 只需要在這裏編輯

try: 
    page = int(request.args.get('page', 1)) 
except ValueError: 
    page = 1 

List=fs.list() 
i=(page-1)*PER_PAGE 
List1=List[i:i+5] 
pagination = Pagination(page=page,per_page=PER_PAGE, total=len(List), search=search, record_name='List') 
return render_template("retrieveFile.html",List=List1,fs=fs,form="submitIt",pagination=pagination,) 
+0

現在的問題是我收到的鏈接水平 –

2

超級晚的答案,但希望這澄清:

除了捕獲頁面和per_page值之外,還需要偏移以幫助正確顯示每個頁面上的結果。實際上有一個方便的built-in設置所有三個,你可以使用from flask_paginate import get_page_args導入。下面是完整的view.py代碼會是什麼樣子:

from flask_paginate import Pagination, get_page_args 

@search.route('/retrieve_data') 
def retrieve(): 
    # get_page_arg defaults to page 1, per_page of 10 
    page, per_page, offset = get_page_args() 

    # After the main query, you need to apply the per_page limit and offset 
    fs = gridfs.GridFS(db) 
    fs_for_render = fs.limit(per_page).offset(offset) 

    #you can also add css_framework='bootstrap3' to Pagination for easy styling 
    pagination = Pagination(page=page, per_page=per_page, offset=offset, 
          total=fs.count(), record_name='List') 

    return render_template('retrieveFile.html', fs=fs_for_render, pagination=pagination, 
          form="submitIt") 
+0

此行讓我大大fs_for_render = fs.limit(per_page).offset(偏移)。你是互聯網上的英雄。 – Anekdotin