def get_posts(post_id): post = Post.query.get(post_id) form = forms.PostForm() form.title.data = post.title form.content.data = post.content posts = Post.query.all() return render_template('post.html', title=post.title, post=post, form=form)
def post_update(post_id): post = Post.query.get(post_id) if post.user_id != current_user.id: abort(403) form = forms.PostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data db.session.commit() flash('Post updated successfuly', 'success') return redirect(url_for('posts.get_posts', post_id=post_id))
def home(): form = forms.PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, user_id=current_user.id) db.session.add(post) db.session.commit() flash('Post created successfuly', 'success') return redirect(url_for('main.home')) posts = Post.query.all() return render_template('home.html', title='Home', posts=posts, form=form)
def newPost(): form = forms.PostForm() if form.validate_on_submit(): post = models.Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flask.flash(f'Post created!', 'success') return flask.redirect(flask.url_for('home')) return flask.render_template('managePost.html', title="New Post", form=form)
def updatePost(post_id): post = models.Post.query.get_or_404(post_id) if post.author != current_user: flask.abort(403) form = forms.PostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data db.session.commit() flask.flash(f'Post Update!', 'success') return flask.redirect(flask.url_for('home')) elif flask.request.method == 'GET': form.title.data = post.title form.content.data = post.content return flask.render_template('managePost.html', title="Update Post", post=post, form=form)