小编Eri*_*ric的帖子

使用WTForms和Flask预填充编辑表单

我可以使用WTForms和Flask向我的数据库添加一个新条目,我也可以编辑,问题是我需要以编辑形式显示数据库中已有的信息.

我有以下代码:

编辑帖子表格的类

class editPostForm(Form):
    postTitle = TextField('postTitle', validators.Required()])
    postSubtitle = TextField('postSubtitle', validators.Required()])
Run Code Online (Sandbox Code Playgroud)

编辑帖子模板的路径

@app.route('/editpost/<postId>', methods = ['GET','POST'])
def editpost_page(postId):
try:
    form = editPostForm(form)

    if request.method == "POST" and form.validate():
        postTitle = form.postTitle.data
        postSubtitle = form.postSubtitle.data

        c, conn = connection()

        query = c.execute("SELECT * FROM posts WHERE post_id = (%s)",
                          [noinjection(postId)])

        c.execute("UPDATE posts SET post_title=%s, post_subtitle=%s WHERE post_id = %s",
                     [
                     noinjection(postTitle),
                     noinjection(postSubtitle),
                     noinjection(postId)
                     ])

        conn.commit()

        flash("Post Edited", 'success')

        c.close()
        conn.close()
        gc.collect()

        return redirect(url_for('posts'))

    return render_template("editpost.html", form = form, POST_ID …
Run Code Online (Sandbox Code Playgroud)

python jinja2 flask flask-wtforms flask-bootstrap

6
推荐指数
2
解决办法
8888
查看次数

标签 统计

flask ×1

flask-bootstrap ×1

flask-wtforms ×1

jinja2 ×1

python ×1