簡體   English   中英

如何解決 Python Flask 中的接口綁定錯誤

[英]How Do I solve Interface Binding Error In Python Flask

我在嘗試將我的 PostForm 與發布的圖片上傳能力綁定時遇到問題,但是有一個功能性的個人資料圖片編碼可以接受圖片並將名稱保存到數據庫中,為了避免錯誤,它會將 uuid1 添加到圖片名稱中它保存在數據庫中,圖像本身保存在靜態文件夾中,在需要時調用它的圖像部分,用戶窗體的所有部分,但對於 PostForm,方法是相同的,但不是配置文件圖片,這是一個應該出現在那里的帖子圖像,它一直給我界面錯誤,但是如果圖像被排除,則帖子已成功添加到數據庫中,請我需要你的幫助才能知道出了什么問題,這是代碼:

@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)

經過數周的嘗試和錯誤,我終於掌握了竅門……我將在下面發布答案:

@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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM