如何解决 Python Flask 中的接口绑定错误

How Do I solve Interface Binding Error In Python Flask

我在尝试将我的 Post 表单与图像上传功能绑定到 Post 时遇到问题,但是有一个功能性的个人资料图片编码可以接受图像并将名称保存到数据库并为避免错误,它将 uuid1 添加到保存在数据库中的图像名称中,图像本身保存在静态文件夹中,需要时调用的图像部分,用户窗体的所有部分,但对于Post表格,方法是一样的,但它不是个人资料图片,而是应该出现在那里的 post 图像,它一直给我界面错误,但是如果图像被排除在外, Post 已成功添加到数据库中,我需要您的帮助才能知道出了什么问题,这是代码:

@app.route('/add-post', methods=['GET', 'POST'])
def add_post():
    form = PostForm()

    if form.validate_on_submit():

        if request.files['post_image']:
            post_image = request.files['post_image']
            post_filename = secure_filename(post_image.filename)
            post_name = str(uuid.uuid1()) + "_" + post_filename         
            saver = request.files['post_image']
            post_image = str(uuid.uuid1()) + "_" + post_filename

            try:
                db.session.commit()
                saver.save(os.path.join(app.config['UPLOAD_FOLDER'], post_name))

            except:
                flash("Error! Looks Like There Was a Problem... Try Again!")

        else:
            db.session.commit()
                

        poster = current_user.id
        post = Posts(title=form.title.data, post_image=form.post_image.data, content=form.content.data, poster_id=poster, slug=form.slug.data)
        
        form.title.data = ''
        form.content.data = ''
        form.slug.data = ''
        form.post_image.data = ''

        db.session.add(post)
        db.session.commit()

        flash("Blog Post Submitted Successfully !")

    return render_template("add_post.html", form=form)

经过数周的尝试和错误,我终于掌握了窍门...我将 post 下面的答案:

@app.route('/add-post', methods=['GET', 'POST'])
def add_post():
    form = PostForm()
            
    if form.validate_on_submit():

        poster = current_user.id
        post = Posts(title=form.title.data, content=form.content.data, file=form.file.data, poster_id=poster, slug=form.slug.data)

        if request.files['file']:
            upload.file = request.files['file']

            filename = secure_filename(upload.file.filename)

            file_name = str(uuid.uuid1()) + "_" + filename
            
            saver = request.files['file']

            post.file = file_name

            try:
                db.session.add(post)
                db.session.commit()
                saver.save(os.path.join(app.config['UPLOAD_FOLDER'], file_name))
                flash("User Updated Successfully !")
                return render_template("add_post.html",
                    form=form)
            except:
                flash("Error! Looks Like There Was a Problem... Try Again!")
                return render_template("add_post.html",
                    form=form)
        else:
            db.session.add(post)
            db.session.commit()
            flash("User Updated Successfully !")
            return render_template("add_post.html",
                form=form)
        
        form.title.data = ''
        form.content.data = ''
        form.slug.data = ''

        db.session.add(post)
        db.session.commit()

    return render_template("add_post.html",
            form=form,
            id = id or 1)