Skip to content

Commit

Permalink
Added pagination to user and index
Browse files Browse the repository at this point in the history
  • Loading branch information
JayKayAce committed Jul 16, 2019
1 parent 082e1da commit 9aea5c8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
41 changes: 28 additions & 13 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,25 @@ def index():
page = request.args.get("page", 1, type=int)
posts = current_user.followed_posts().paginate(
page, app.config["POSTS_PER_PAGE"], False)
return render_template('index.html', title = "Home Page", form = form, posts=posts.items)
next_url = url_for("index", page = posts.next_num) if post.has_next else None
prev_url = url_for("index", page = posts.prev_num) if posts.has_prev else None
return render_template('index.html',
title = "Home Page", 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"])
def login():
Expand Down Expand Up @@ -78,11 +96,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 post.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,
next_url = next_url, prev_url=prev_url)

@app.before_request
def before_request():
Expand Down Expand Up @@ -136,10 +158,3 @@ def unfollow(username):
flash(f"you are not following {username} anymore")
return redirect(url_for("user", username = username))

@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)
return render_template("index.html",title="Explore", posts=posts.items)
6 changes: 6 additions & 0 deletions app/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ <h1>Hi {{ current_user.username }}</h1>
{% for post in posts %}
{% include '_post.html' %}
{% endfor %}
{% if prev_url %}
<a href="{{ prev_url }}">Newer Posts</a>
{% endif %}
{% if next_url %}
<a href="{{ next_url }}">Older Posts</a>
{% endif %}
{% endblock%}
6 changes: 6 additions & 0 deletions app/templates/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ <h1>User: {{ user.username }}</h1>
{% for post in posts %}
{% include "_post.html" %}
{% endfor %}
{% if prev_url %}
<a href="{{ prev_url }}">Newer Posts</a>
{% endif %}
{% if next_url %}
<a href="{{ next_url }}">Older Posts</a>
{% endif %}
{% endblock %}
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ class Config:
MAIL_USERNAME = os.environ.get("MAIL_USERNAME")
MAIL_PASSWORD = os.environ.get("MAIL_PASSWORD")
ADMINS = ["[email protected]"]
POSTS_PER_PAGE = 3
POSTS_PER_PAGE = 25

0 comments on commit 9aea5c8

Please sign in to comment.