pagination

This commit is contained in:
Miguel Grinberg 2012-12-16 00:34:46 -08:00
parent 47e65873a9
commit 1886654ff9
6 changed files with 60 additions and 30 deletions

View File

@ -25,4 +25,6 @@ class EditForm(Form):
return False return False
return True return True
class PostForm(Form):
post = TextField('post', validators = [Required()])

View File

@ -77,4 +77,4 @@ class Post(db.Model):
user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
def __repr__(self): def __repr__(self):
return '<Post %r>' % (self.body) return '<Post %r>' % (self.text)

View File

@ -2,10 +2,29 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block content %} {% block content %}
<h1>Hi, {{user.nickname}}!</h1> <h1>Hi, {{g.user.nickname}}!</h1>
{% for post in posts %} <form action="" method="post" name="post">
<p> {{form.hidden_tag()}}
{{post.author.nickname}} says: <b>{{post.body}}</b> <table>
</p> <tr>
<td>Say something:</td>
<td>{{ form.post(size = 30, maxlength = 140) }}</td>
<td>
{% for error in form.errors.post %}
<span style="color: red;">[{{error}}]</span><br>
{% endfor %} {% endfor %}
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Post!"></td>
<td></td>
</tr>
</table>
</form>
{% for post in posts.items %}
{% include 'post.html' %}
{% endfor %}
{% 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 %}
{% endblock %} {% endblock %}

View File

@ -22,7 +22,9 @@
</tr> </tr>
</table> </table>
<hr> <hr>
{% for post in posts %} {% for post in posts.items %}
{% include 'post.html' %} {% include 'post.html' %}
{% endfor %} {% endfor %}
{% if posts.has_prev %}<a href="{{ url_for('user', nickname = user.nickname, page = posts.prev_num) }}"><< Newer posts</a>{% else %}<< Newer posts{% endif %} |
{% if posts.has_next %}<a href="{{ url_for('user', nickname = user.nickname, page = posts.next_num) }}">Older posts >></a>{% else %}Older posts >>{% endif %}
{% endblock %} {% endblock %}

View File

@ -1,9 +1,10 @@
from flask import render_template, flash, redirect, session, url_for, request, g from flask import render_template, flash, redirect, session, url_for, request, g
from flask.ext.login import login_user, logout_user, current_user, login_required from flask.ext.login import login_user, logout_user, current_user, login_required
from app import app, db, lm, oid from app import app, db, lm, oid
from forms import LoginForm, EditForm from forms import LoginForm, EditForm, PostForm
from models import User, ROLE_USER, ROLE_ADMIN from models import User, ROLE_USER, ROLE_ADMIN, Post
from datetime import datetime from datetime import datetime
from config import POSTS_PER_PAGE
@lm.user_loader @lm.user_loader
def load_user(id): def load_user(id):
@ -26,24 +27,22 @@ def internal_error(error):
db.session.rollback() db.session.rollback()
return render_template('500.html'), 500 return render_template('500.html'), 500
@app.route('/') @app.route('/', methods = ['GET', 'POST'])
@app.route('/index') @app.route('/index', methods = ['GET', 'POST'])
@app.route('/index/<int:page>', methods = ['GET', 'POST'])
@login_required @login_required
def index(): def index(page = 1):
user = g.user form = PostForm()
posts = [ if form.validate_on_submit():
{ post = Post(body = form.post.data, timestamp = datetime.utcnow(), author = g.user)
'author': { 'nickname': 'John' }, db.session.add(post)
'body': 'Beautiful day in Portland!' db.session.commit()
}, flash('Your post is now live!')
{ return redirect(url_for('index'))
'author': { 'nickname': 'Susan' }, posts = g.user.followed_posts().paginate(page, POSTS_PER_PAGE, False)
'body': 'The Avengers movie was so cool!'
}
]
return render_template('index.html', return render_template('index.html',
title = 'Home', title = 'Home',
user = user, form = form,
posts = posts) posts = posts)
@app.route('/login', methods = ['GET', 'POST']) @app.route('/login', methods = ['GET', 'POST'])
@ -89,16 +88,14 @@ def logout():
return redirect(url_for('index')) return redirect(url_for('index'))
@app.route('/user/<nickname>') @app.route('/user/<nickname>')
@app.route('/user/<nickname>/<int:page>')
@login_required @login_required
def user(nickname): def user(nickname, page = 1):
user = User.query.filter_by(nickname = nickname).first() user = User.query.filter_by(nickname = nickname).first()
if user == None: if user == None:
flash('User ' + nickname + ' not found.') flash('User ' + nickname + ' not found.')
return redirect(url_for('index')) return redirect(url_for('index'))
posts = [ posts = user.posts.paginate(page, POSTS_PER_PAGE, False)
{ 'author': user, 'body': 'Test post #1' },
{ 'author': user, 'body': 'Test post #2' }
]
return render_template('user.html', return render_template('user.html',
user = user, user = user,
posts = posts) posts = posts)
@ -126,6 +123,9 @@ def follow(nickname):
if user == None: if user == None:
flash('User ' + nickname + ' not found.') flash('User ' + nickname + ' not found.')
return redirect(url_for('index')) return redirect(url_for('index'))
if user == g.user:
flash('You can\'t follow yourself!')
return redirect(url_for('user', nickname = nickname))
u = g.user.follow(user) u = g.user.follow(user)
if u is None: if u is None:
flash('Cannot follow ' + nickname + '.') flash('Cannot follow ' + nickname + '.')
@ -141,6 +141,9 @@ def unfollow(nickname):
if user == None: if user == None:
flash('User ' + nickname + ' not found.') flash('User ' + nickname + ' not found.')
return redirect(url_for('index')) return redirect(url_for('index'))
if user == g.user:
flash('You can\'t unfollow yourself!')
return redirect(url_for('user', nickname = nickname))
u = g.user.unfollow(user) u = g.user.unfollow(user)
if u is None: if u is None:
flash('Cannot unfollow ' + nickname + '.') flash('Cannot unfollow ' + nickname + '.')

View File

@ -22,3 +22,7 @@ MAIL_PASSWORD = None
# administrator list # administrator list
ADMINS = ['you@example.com'] ADMINS = ['you@example.com']
# pagination
POSTS_PER_PAGE = 3