Chapter 8

This commit is contained in:
2023-06-04 01:17:48 +02:00
parent d81f038422
commit c91b89da5f
6 changed files with 44 additions and 19 deletions

View File

@@ -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)