2013-04-04 2 views
8

내가 블로그 게시물을 렌더링 작은 플라스크 응용 프로그램이 있습니다플라스크 mongoengine 페이지 매김

views.py :

class ListView(MethodView): 

    def get(self, page=1): 
     posts = Post.objects.all() 
     return render_template('posts/list.html', posts=posts) 

이 모든 좋은,하지만 난 posts 객체에 페이지 매김을 추가하고 싶습니다 . project docs을 보면 페이지 매김 클래스가 있다는 것을 알았습니다.

그래서 나는이 시도 :

class ListView(MethodView): 

    def get(self, page=1): 
     posts = Post.objects.paginate(page=page, per_page=10) 
     return render_template('posts/list.html', posts=posts) 

을하지만 지금은 오류 얻을 :를 통해 내가 반복 어떻게 그래서

TypeError: 'Pagination' object is not iterable 

을 내 템플릿에 posts을?

도움을 주시면 감사하겠습니다.

+1

임시 직원 늦은 코드? 공유 할 수 있습니까? – codegeek

답변

8

Pagination 개체는 items list이며 몽고 엔진 문서 개체 (귀하의 경우 Post 개체)를 포함합니다. 이 목록은 문서를 표시 할 때까지 반복 할 수 있습니다.

예를 들어, 템플릿 :

{% for post in posts.items %} 
    {{ post.title }} 
    {{ post.content }} 
{% endfor %} 

는 매김 링크의 실제 페이지 번호를 얻을 사용 iter_pages() :

<div id="pagination-links"> 
    {% for page in posts.iter_pages() %} 
     {{ page }} 
    {% endfor %} 
</div> 

모두 documentationgithub link above는 더 나은 예를 들어,이 페이지 매김 링크 :

{% macro render_pagination(pagination, endpoint) %} 
    <div class=pagination> 
     {%- for page in pagination.iter_pages() %} 
      {% if page %} 
       {% if page != pagination.page %} 
        <a href="{{ url_for(endpoint, page=page) }}">{{ page }}</a> 
       {% else %} 
        <strong>{{ page }}</strong> 
       {% endif %} 
      {% else %} 
       <span class=ellipsis>…</span> 
      {% endif %} 
     {%- endfor %} 
    </div> 
{% endmacro %} 
+0

최신 플라스크 - 몽고 엔진 방출을 반영하기 위해 github 링크를 업데이트했습니다. –

관련 문제