def edit_post(id): if request.method == 'POST': form = PostForm() form.tag.choices = [(str(tag.id), str(tag.tag)) for tag in Tag.query.all()] if form.validate() == False: return render_template('editpost.html', form=form) else: tags = [ Tag.query.filter_by(id=tag_id).first() for tag_id in form.tag.data ] post = Post.query.filter_by(id=id).first() post.title = form.title.data post.text = form.text.data post.tags = tags db.session.merge(post) db.session.commit() flash('Post updated successfully') return render_template('index.html') elif request.method == 'GET': post = Post.query.filter_by(id=id).first() form = PostForm(id=post.id, title=post.title, text=post.text) form.tag.choices = [(str(tag.id), str(tag.tag)) for tag in Tag.query.all()] return render_template('editpost.html', post_id=post.id, form=form)
def addpost(): form = PostForm(csrf_enabled=False) form.tag.choices = [(str(tag.id), str(tag.tag)) for tag in Tag.query.all()] if request.method == "POST": if form.validate() == False: return render_template('addpost.html', form = form) else: tags = [ Tag.query.filter_by(id=tag_id).first() for tag_id in form.tag.data ] post = Post(form.title.data, form.text.data, tags) db.session.add(post) db.session.commit() flash('Posted successfully') return render_template('index.html') return render_template("addpost.html", form = form)
def addpost(): form = PostForm(csrf_enabled=False) form.tag.choices = [(str(tag.id), str(tag.tag)) for tag in Tag.query.all()] if request.method == "POST": if form.validate() == False: return render_template('addpost.html', form=form) else: tags = [ Tag.query.filter_by(id=tag_id).first() for tag_id in form.tag.data ] post = Post(form.title.data, form.text.data, tags) db.session.add(post) db.session.commit() flash('Posted successfully') return render_template('index.html') return render_template("addpost.html", form=form)
def edit_post(id): if request.method == 'POST': form = PostForm() form.tag.choices = [(str(tag.id), str(tag.tag)) for tag in Tag.query.all()] if form.validate() == False: return render_template('editpost.html', form=form) else: tags = [ Tag.query.filter_by(id=tag_id).first() for tag_id in form.tag.data ] post = Post.query.filter_by(id=id).first() post.title = form.title.data post.text = form.text.data post.tags = tags db.session.merge(post) db.session.commit() flash('Post updated successfully') return render_template('index.html') elif request.method == 'GET': post = Post.query.filter_by(id=id).first() form = PostForm(id=post.id, title=post.title, text=post.text) form.tag.choices = [(str(tag.id), str(tag.tag)) for tag in Tag.query.all()] return render_template('editpost.html', post_id= post.id, form=form)