mirror of
https://github.com/rmontanana/microblog.git
synced 2025-08-17 16:25:51 +00:00
Chapter 8
This commit is contained in:
@@ -61,3 +61,8 @@ class EditProfileForm(FlaskForm):
|
|||||||
|
|
||||||
class EmptyForm(FlaskForm):
|
class EmptyForm(FlaskForm):
|
||||||
submit = SubmitField("Submit")
|
submit = SubmitField("Submit")
|
||||||
|
|
||||||
|
class PostForm(FlaskForm):
|
||||||
|
post = TextAreaField('Say something', validators=[
|
||||||
|
DataRequired(), Length(min=1, max=140)])
|
||||||
|
submit = SubmitField('Submit')
|
@@ -3,8 +3,8 @@ from flask import render_template, flash, redirect, url_for, request
|
|||||||
from flask_login import current_user, login_user, logout_user, login_required
|
from flask_login import current_user, login_user, logout_user, login_required
|
||||||
from werkzeug.urls import url_parse
|
from werkzeug.urls import url_parse
|
||||||
from app import app, db
|
from app import app, db
|
||||||
from app.forms import LoginForm, RegistrationForm, EditProfileForm, EmptyForm
|
from app.forms import LoginForm, RegistrationForm, EditProfileForm, EmptyForm, PostForm
|
||||||
from app.models import User
|
from app.models import User, Post
|
||||||
|
|
||||||
|
|
||||||
@app.before_request
|
@app.before_request
|
||||||
@@ -14,18 +14,20 @@ def before_request():
|
|||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route('/', methods=['GET', 'POST'])
|
||||||
@app.route("/index")
|
@app.route('/index', methods=['GET', 'POST'])
|
||||||
@login_required
|
@login_required
|
||||||
def index():
|
def index():
|
||||||
posts = [
|
form = PostForm()
|
||||||
{"author": {"username": "John"}, "body": "Beautiful day in Portland!"},
|
if form.validate_on_submit():
|
||||||
{
|
post = Post(body=form.post.data, author=current_user)
|
||||||
"author": {"username": "Susan"},
|
db.session.add(post)
|
||||||
"body": "The Avengers movie was so cool!",
|
db.session.commit()
|
||||||
},
|
flash('Your post is now live!')
|
||||||
]
|
return redirect(url_for('index'))
|
||||||
return render_template("index.html", title="Home", posts=posts)
|
posts = current_user.followed_posts().all()
|
||||||
|
return render_template("index.html", title='Home Page', form=form,
|
||||||
|
posts=posts)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/login", methods=["GET", "POST"])
|
@app.route("/login", methods=["GET", "POST"])
|
||||||
@@ -75,7 +77,8 @@ def user(username):
|
|||||||
{"author": user, "body": "Test post #1"},
|
{"author": user, "body": "Test post #1"},
|
||||||
{"author": user, "body": "Test post #2"},
|
{"author": user, "body": "Test post #2"},
|
||||||
]
|
]
|
||||||
return render_template("user.html", user=user, posts=posts)
|
form = EmptyForm()
|
||||||
|
return render_template("user.html", user=user, posts=posts, form=form)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/edit_profile", methods=["GET", "POST"])
|
@app.route("/edit_profile", methods=["GET", "POST"])
|
||||||
@@ -134,3 +137,9 @@ def unfollow(username):
|
|||||||
return redirect(url_for("user", username=username))
|
return redirect(url_for("user", username=username))
|
||||||
else:
|
else:
|
||||||
return redirect(url_for("index"))
|
return redirect(url_for("index"))
|
||||||
|
|
||||||
|
@app.route('/explore')
|
||||||
|
@login_required
|
||||||
|
def explore():
|
||||||
|
posts = Post.query.order_by(Post.timestamp.desc()).all()
|
||||||
|
return render_template('index.html', title='Explore', posts=posts)
|
@@ -4,7 +4,8 @@
|
|||||||
<img src="{{ post.author.avatar(36) }}" />
|
<img src="{{ post.author.avatar(36) }}" />
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
{{ post.author.username }} says:<br />{{ post.body }}
|
<a href="{{ url_for('user', username = post.author.username) }}">{{ post.author.username }}</a>
|
||||||
|
says:<br />{{ post.body }}
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
@@ -14,6 +14,7 @@
|
|||||||
<a href="{{ url_for('login') }}">Login</a>
|
<a href="{{ url_for('login') }}">Login</a>
|
||||||
{% else %}
|
{% else %}
|
||||||
<a href="{{ url_for('user', username = current_user.username) }}">Profile</a>
|
<a href="{{ url_for('user', username = current_user.username) }}">Profile</a>
|
||||||
|
<a href="{{ url_for('explore') }}">Explore</a>
|
||||||
<a href="{{ url_for('logout') }}">Logout</a>
|
<a href="{{ url_for('logout') }}">Logout</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
@@ -2,11 +2,20 @@
|
|||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>Hi, {{ current_user.username }}!</h1>
|
<h1>Hi, {{ current_user.username }}!</h1>
|
||||||
{% for post in posts %}
|
{% if form %}
|
||||||
<div>
|
<form action="" method="post">
|
||||||
|
{{ form.hidden_tag() }}
|
||||||
<p>
|
<p>
|
||||||
{{ post.author.username }} says: <b>{{ post.body }}</b>
|
{{ form.post.label }}<br />
|
||||||
</p>
|
{{ form.post(cols = 32, rows = 4) }}<br />
|
||||||
</div>
|
{% for error in form.post.errors %}
|
||||||
|
<span style="color: red;">[{{ error }}]</span>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endblock %}
|
</p>
|
||||||
|
<p>{{ form.submit() }}</p>
|
||||||
|
</form>
|
||||||
|
{% endif %}
|
||||||
|
{% for post in posts %}
|
||||||
|
{% include "_post.html" %}
|
||||||
|
{% endfor %}
|
||||||
|
{% endblock %}s
|
||||||
|
Reference in New Issue
Block a user