0

I have a view:

@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
@app.route('/index/<int:page>', methods=['GET', 'POST'])
def index(page=1):
 posts = Post.query.paginate(page, 3, False).items
 return render_template('index.html', posts=posts)

Code template:

 {% if posts %}
 <ol>
 {% for post in posts %}
 <li>{{ post.title }}</li> | {{ post.text }} | {{ post.time }}</li>
 {% endfor %}
 </ol>
 {% else %}
 <h2>There is no posts</h2>
 {% endif %}
 {% if posts.has_prev %}<a href="{{ url_for('index', page=posts.prev_num) }}"><< Newer posts</a>{% else %}<< Newer posts{% endif %} |
 {% if posts.has_next %}<a href="{{ url_for('index', page=posts.next_num) }}">Older posts >></a>{% else %}Older posts >>{% endif %}

The problem is that it does not generate me a link to see more posts.

asked Oct 25, 2014 at 12:46

1 Answer 1

5

.has_prev and .has_next are attributes of the Pagination instance, but you are discarding that instance. All you passed into the view is the Pagination.items attribute. You are then trying to still access the .has_prev and .has_next attributes on the items list, and those attributes do not exist there so Jinja2 resolves those as undefined.

Pass in the Pagination instance:

posts = Post.query.paginate(page, 3, False)
return render_template('index.html', posts=posts)

and adjust your template:

{% if posts.items %}
 <ol>
 {% for post in posts.items %}
 <li>{{ post.title }}</li> | {{ post.text }} | {{ post.time }}</li>
 {% endfor %}
</ol>
{% else %}
 <h2>There is no posts</h2>
{% endif %}
{% if posts.has_prev %}<a href="{{ url_for('index', page=posts.prev_num) }}"><< Newer posts</a>{% else %}<< Newer posts{% endif %} |
{% if posts.has_next %}<a href="{{ url_for('index', page=posts.next_num) }}">Older posts >></a>{% else %}Older posts >>{% endif %}
answered Oct 25, 2014 at 12:58
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.