Reputation: 2031
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.
Upvotes: 0
Views: 1707
Reputation: 1121844
.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 %}
Upvotes: 5