From f525e4c270e4f61ba90174fd7dc735d4f8868ba7 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Sun, 17 Sep 2017 22:41:40 -0700 Subject: [PATCH] Chapter 9: Pagination (v0.9) --- app/forms.py | 5 ++++ app/routes.py | 64 +++++++++++++++++++++++++++------------- app/templates/_post.html | 2 +- app/templates/base.html | 3 +- app/templates/index.html | 21 ++++++++++++- app/templates/user.html | 6 ++++ config.py | 1 + 7 files changed, 79 insertions(+), 23 deletions(-) diff --git a/app/forms.py b/app/forms.py index f85a757..90b6f2b 100644 --- a/app/forms.py +++ b/app/forms.py @@ -46,3 +46,8 @@ class EditProfileForm(FlaskForm): user = User.query.filter_by(username=self.username.data).first() if user is not None: raise ValidationError('Please use a different username.') + + +class PostForm(FlaskForm): + post = TextAreaField('Say something', validators=[DataRequired()]) + submit = SubmitField('Submit') diff --git a/app/routes.py b/app/routes.py index a0e0a8b..010e5df 100644 --- a/app/routes.py +++ b/app/routes.py @@ -3,8 +3,8 @@ from flask import render_template, flash, redirect, url_for, request from flask_login import login_user, logout_user, current_user, login_required from werkzeug.urls import url_parse from app import app, db -from app.forms import LoginForm, RegistrationForm, EditProfileForm -from app.models import User +from app.forms import LoginForm, RegistrationForm, EditProfileForm, PostForm +from app.models import User, Post @app.before_request @@ -14,21 +14,41 @@ def before_request(): db.session.commit() -@app.route('/') -@app.route('/index') +@app.route('/', methods=['GET', 'POST']) +@app.route('/index', methods=['GET', 'POST']) @login_required def index(): - posts = [ - { - 'author': {'username': 'John'}, - 'body': 'Beautiful day in Portland!' - }, - { - 'author': {'username': 'Susan'}, - 'body': 'The Avengers movie was so cool!' - } - ] - return render_template('index.html', title='Home', posts=posts) + form = PostForm() + if form.validate_on_submit(): + post = Post(body=form.post.data, author=current_user) + db.session.add(post) + db.session.commit() + flash('Your post is now live!') + return redirect(url_for('index')) + page = request.args.get('page', 1, type=int) + posts = current_user.followed_posts().paginate( + page, app.config['POSTS_PER_PAGE'], False) + next_url = url_for('explore', page=posts.next_num) \ + if posts.has_next else None + prev_url = url_for('explore', page=posts.prev_num) \ + if posts.has_prev else None + return render_template('index.html', title='Home', form=form, + posts=posts.items, next_url=next_url, + prev_url=prev_url) + + +@app.route('/explore') +@login_required +def explore(): + page = request.args.get('page', 1, type=int) + posts = Post.query.order_by(Post.timestamp.desc()).paginate( + page, app.config['POSTS_PER_PAGE'], False) + next_url = url_for('explore', page=posts.next_num) \ + if posts.has_next else None + prev_url = url_for('explore', page=posts.prev_num) \ + if posts.has_prev else None + return render_template('index.html', title='Explore', posts=posts.items, + next_url=next_url, prev_url=prev_url) @app.route('/login', methods=['GET', 'POST']) @@ -74,11 +94,15 @@ def register(): @login_required def user(username): user = User.query.filter_by(username=username).first_or_404() - posts = [ - {'author': user, 'body': 'Test post #1'}, - {'author': user, 'body': 'Test post #2'} - ] - return render_template('user.html', user=user, posts=posts) + page = request.args.get('page', 1, type=int) + posts = user.posts.order_by(Post.timestamp.desc()).paginate( + page, app.config['POSTS_PER_PAGE'], False) + next_url = url_for('user', username=user.username, page=posts.next_num) \ + if posts.has_next else None + prev_url = url_for('user', username=user.username, page=posts.prev_num) \ + if posts.has_prev else None + return render_template('user.html', user=user, posts=posts.items, + next_url=next_url, prev_url=prev_url) @app.route('/edit_profile', methods=['GET', 'POST']) diff --git a/app/templates/_post.html b/app/templates/_post.html index d020426..a705b31 100644 --- a/app/templates/_post.html +++ b/app/templates/_post.html @@ -1,6 +1,6 @@ - +
{{ post.author.username }} says:
{{ post.body }}
{{ post.author.username }} says:
{{ post.body }}
diff --git a/app/templates/base.html b/app/templates/base.html index 0c81af2..42a98ce 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -10,6 +10,7 @@
Microblog: Home + Explore {% if current_user.is_anonymous %} Login {% else %} @@ -28,5 +29,5 @@ {% endif %} {% endwith %} {% block content %}{% endblock %} - + diff --git a/app/templates/index.html b/app/templates/index.html index 2565a01..eb3f876 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -2,7 +2,26 @@ {% block content %}

Hi, {{ current_user.username }}!

+ {% if form %} +
+ {{ form.hidden_tag() }} +

+ {{ form.post.label }}
+ {{ form.post(cols=32, rows=4) }}
+ {% for error in form.post.errors %} + [{{ error }}] + {% endfor %} +

+

{{ form.submit() }}

+
+ {% endif %} {% for post in posts %} -

{{ post.author.username }} says: {{ post.body }}

+ {% include '_post.html' %} {% endfor %} + {% if prev_url %} + Newer posts + {% endif %} + {% if next_url %} + Older posts + {% endif %} {% endblock %} diff --git a/app/templates/user.html b/app/templates/user.html index 3dca54b..bcb0626 100644 --- a/app/templates/user.html +++ b/app/templates/user.html @@ -23,4 +23,10 @@ {% for post in posts %} {% include '_post.html' %} {% endfor %} + {% if prev_url %} + Newer posts + {% endif %} + {% if next_url %} + Older posts + {% endif %} {% endblock %} diff --git a/config.py b/config.py index a3d7212..d3c5435 100644 --- a/config.py +++ b/config.py @@ -13,3 +13,4 @@ class Config(object): MAIL_USERNAME = os.environ.get('MAIL_USERNAME') MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD') ADMINS = ['your-email@example.com'] + POSTS_PER_PAGE = 25