def add_post(): form = AddPostForm() form.tags.choices = Tag.get_tags_for_form() if form.validate_on_submit(): # Assuming POST method title = request.form.get('title') text = request.form.get('text') image = request.files['image'] new_post = Post(title=title, text=text, author_id=current_user.id, image=image) db.session.add(new_post) tag_ids = form.tags.data if tag_ids: new_post.add_tags_by_id(tag_ids) try: db.session.commit() except Exception as e: logger.exception('Could not add new post.') raise InternalServerError(f'Could not add new post: {e}') return redirect(url_for('posts_app.view_post', pk=new_post.id)) return render_template('posts/add_post.html', form=form)