mirror of
https://github.com/rmontanana/microblog.git
synced 2025-08-16 15:55:51 +00:00
Chapter 8
This commit is contained in:
@@ -61,3 +61,8 @@ class EditProfileForm(FlaskForm):
|
||||
|
||||
class EmptyForm(FlaskForm):
|
||||
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 werkzeug.urls import url_parse
|
||||
from app import app, db
|
||||
from app.forms import LoginForm, RegistrationForm, EditProfileForm, EmptyForm
|
||||
from app.models import User
|
||||
from app.forms import LoginForm, RegistrationForm, EditProfileForm, EmptyForm, PostForm
|
||||
from app.models import User, Post
|
||||
|
||||
|
||||
@app.before_request
|
||||
@@ -14,18 +14,20 @@ 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'))
|
||||
posts = current_user.followed_posts().all()
|
||||
return render_template("index.html", title='Home Page', form=form,
|
||||
posts=posts)
|
||||
|
||||
|
||||
@app.route("/login", methods=["GET", "POST"])
|
||||
@@ -75,7 +77,8 @@ def user(username):
|
||||
{"author": user, "body": "Test post #1"},
|
||||
{"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"])
|
||||
@@ -134,3 +137,9 @@ def unfollow(username):
|
||||
return redirect(url_for("user", username=username))
|
||||
else:
|
||||
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) }}" />
|
||||
</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>
|
||||
</tr>
|
||||
</table>
|
||||
|
@@ -14,6 +14,7 @@
|
||||
<a href="{{ url_for('login') }}">Login</a>
|
||||
{% else %}
|
||||
<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>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
@@ -2,11 +2,20 @@
|
||||
|
||||
{% block content %}
|
||||
<h1>Hi, {{ current_user.username }}!</h1>
|
||||
{% for post in posts %}
|
||||
<div>
|
||||
{% if form %}
|
||||
<form action="" method="post">
|
||||
{{ form.hidden_tag() }}
|
||||
<p>
|
||||
{{ post.author.username }} says: <b>{{ post.body }}</b>
|
||||
{{ form.post.label }}<br />
|
||||
{{ form.post(cols = 32, rows = 4) }}<br />
|
||||
{% for error in form.post.errors %}
|
||||
<span style="color: red;">[{{ error }}]</span>
|
||||
{% endfor %}
|
||||
</p>
|
||||
</div>
|
||||
<p>{{ form.submit() }}</p>
|
||||
</form>
|
||||
{% endif %}
|
||||
{% for post in posts %}
|
||||
{% include "_post.html" %}
|
||||
{% endfor %}
|
||||
{% endblock %}
|
||||
{% endblock %}s
|
||||
|
Reference in New Issue
Block a user