Browse Source

Chapter 11: Blog post pagination (11d)

master
T. Meissner 6 years ago
parent
commit
6c8890d350
6 changed files with 64 additions and 5 deletions
  1. +16
    -5
      app/main/views.py
  2. +6
    -0
      app/static/styles.css
  3. +29
    -0
      app/templates/_macros.html
  4. +6
    -0
      app/templates/index.html
  5. +6
    -0
      app/templates/user.html
  6. +1
    -0
      config.py

+ 16
- 5
app/main/views.py View File

@ -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/<username>')
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'])


+ 6
- 0
app/static/styles.css View File

@ -31,3 +31,9 @@ div.post-content {
margin-left: 48px;
min-height: 48px;
}
div.pagination {
width: 100%;
text-align: center;
padding: 0px;
margin: 0px;
}

+ 29
- 0
app/templates/_macros.html View File

@ -0,0 +1,29 @@
{% macro pagination_widget(pagination, endpoint) %}
<ul class="pagination">
<li{% if not pagination.has_prev %} class="disabled"{% endif %}>
<a href="{% if pagination.has_prev %}{{ url_for(endpoint, page=pagination.prev_num, **kwargs) }}{% else %}#{% endif %}">
&laquo;
</a>
</li>
{% for p in pagination.iter_pages() %}
{% if p %}
{% if p == pagination.page %}
<li class="active">
<a href="{{ url_for(endpoint, page = p, **kwargs) }}">{{ p }}</a>
</li>
{% else %}
<li>
<a href="{{ url_for(endpoint, page = p, **kwargs) }}">{{ p }}</a>
</li>
{% endif %}
{% else %}
<li class="disabled"><a href="#">&hellip;</a></li>
{% endif %}
{% endfor %}
<li{% if not pagination.has_next %} class="disabled"{% endif %}>
<a href="{% if pagination.has_next %}{{ url_for(endpoint, page=pagination.next_num, **kwargs) }}{% else %}#{% endif %}">
&raquo;
</a>
</li>
</ul>
{% endmacro %}

+ 6
- 0
app/templates/index.html View File

@ -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 @@
</div>
{% include '_posts.html' %}
{% if pagination %}
<div class="pagination">
{{ macros.pagination_widget(pagination, '.index') }}
</div>
{% endif %}
{% endblock %}

+ 6
- 0
app/templates/user.html View File

@ -1,4 +1,5 @@
{% extends "base.html" %}
{% import "_macros.html" as macros %}
{% block title %}Flasky - {{ user.username }}{% endblock %}
@ -40,4 +41,9 @@
</div>
<h3>Posts by {{ user.username }}</h3>
{% include '_posts.html' %}
{% if pagination %}
<div class="pagination">
{{ macros.pagination_widget(pagination, '.user', username=user.username) }}
</div>
{% endif %}
{% endblock %}

+ 1
- 0
config.py View File

@ -14,6 +14,7 @@ class Config:
FLASKY_MAIL_SENDER = 'Flasky Admin <flasky@example.com>'
FLASKY_ADMIN = os.environ.get('FLASKY_ADMIN')
SQLALCHEMY_TRACK_MODIFICATIONS = False
FLASKY_POSTS_PER_PAGE = 20
@staticmethod
def init_app(app):


Loading…
Cancel
Save