def new_post_form(): user = current_user post_form = NewPostForm() if post_form.validate_on_submit(): # We first create the post, then process the image to give it the correct name new_post = Post(user_id=user.id, username=user.username, image_name='', description=post_form.description.data, tags=post_form.tags.data) # We add and commit so that the post gets attributed an id in the database db.session.add(new_post) db.session.commit() image = post_form.image.data old_filename, file_extension = os.path.splitext(image.filename) image.filename = str(new_post.id) + str(file_extension) new_post.image_name = image.filename images_upload_set.save(image) # We commit again to add the new image name to the post db.session.commit() return redirect(url_for('profile.profile_index')) return render_template('post/create_post.html.jinja2', user=user, post_form=post_form)