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
Mark
2,0314 gold badges17 silver badges20 bronze badges
1 Answer 1
.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
Martijn Pieters
1.1m327 gold badges4.3k silver badges3.5k bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.
lang-py