From 6c8890d350eacc4ca6d1a55ca81b3adf363f30ae Mon Sep 17 00:00:00 2001 From: tmeissner Date: Sat, 17 Nov 2018 23:42:57 +0100 Subject: [PATCH] Chapter 11: Blog post pagination (11d) --- app/main/views.py | 21 ++++++++++++++++----- app/static/styles.css | 6 ++++++ app/templates/_macros.html | 29 +++++++++++++++++++++++++++++ app/templates/index.html | 6 ++++++ app/templates/user.html | 6 ++++++ config.py | 1 + 6 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 app/templates/_macros.html diff --git a/app/main/views.py b/app/main/views.py index bced0bc..bd5c694 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -1,4 +1,5 @@ -from flask import render_template, redirect, url_for, flash +from flask import render_template, redirect, url_for, flash, request, \ + current_app from flask_login import login_required, current_user from . import main from .forms import EditProfileForm, EditProfileAdminForm, PostForm @@ -16,15 +17,25 @@ def index(): db.session.add(post) db.session.commit() return redirect(url_for('.index')) - posts = Post.query.order_by(Post.timestamp.desc()).all() - return render_template('index.html', form=form, posts=posts) + page = request.args.get('page', 1, type=int) + pagination = Post.query.order_by(Post.timestamp.desc()).paginate( + page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'], + error_out=False) + posts = pagination.items + return render_template('index.html', form=form, posts=posts, + pagination=pagination) @main.route('/user/') def user(username): user = User.query.filter_by(username=username).first_or_404() - posts = user.posts.order_by(Post.timestamp.desc()).all() - return render_template('user.html', user=user, posts=posts) + page = request.args.get('page', 1, type=int) + pagination = user.posts.order_by(Post.timestamp.desc()).paginate( + page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'], + error_out=False) + posts = pagination.items + return render_template('user.html', user=user, posts=posts, + pagination=pagination) @main.route('/edit-profile', methods=['GET', 'POST']) diff --git a/app/static/styles.css b/app/static/styles.css index 153c2f2..6b52e69 100644 --- a/app/static/styles.css +++ b/app/static/styles.css @@ -31,3 +31,9 @@ div.post-content { margin-left: 48px; min-height: 48px; } +div.pagination { + width: 100%; + text-align: center; + padding: 0px; + margin: 0px; +} diff --git a/app/templates/_macros.html b/app/templates/_macros.html new file mode 100644 index 0000000..b5d55a3 --- /dev/null +++ b/app/templates/_macros.html @@ -0,0 +1,29 @@ +{% macro pagination_widget(pagination, endpoint) %} + +{% endmacro %} diff --git a/app/templates/index.html b/app/templates/index.html index 4f86cca..8773438 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -1,5 +1,6 @@ {% extends "base.html" %} {% import "bootstrap/wtf.html" as wtf %} +{% import "_macros.html" as macros %} {% block title %}Flasky{% endblock %} @@ -16,4 +17,9 @@ {% include '_posts.html' %} +{% if pagination %} + + {% endif %} {% endblock %} diff --git a/app/templates/user.html b/app/templates/user.html index 472fdf6..4acddaa 100644 --- a/app/templates/user.html +++ b/app/templates/user.html @@ -1,4 +1,5 @@ {% extends "base.html" %} +{% import "_macros.html" as macros %} {% block title %}Flasky - {{ user.username }}{% endblock %} @@ -40,4 +41,9 @@

Posts by {{ user.username }}

{% include '_posts.html' %} + {% if pagination %} + + {% endif %} {% endblock %} diff --git a/config.py b/config.py index 0824dc5..0503a70 100644 --- a/config.py +++ b/config.py @@ -14,6 +14,7 @@ class Config: FLASKY_MAIL_SENDER = 'Flasky Admin ' FLASKY_ADMIN = os.environ.get('FLASKY_ADMIN') SQLALCHEMY_TRACK_MODIFICATIONS = False + FLASKY_POSTS_PER_PAGE = 20 @staticmethod def init_app(app):