我在更新博客文章时遇到问题:
@app.route('/edit_post/<post_id>', methods=['GET', 'POST'])
def edit_post(post_id):
post = db.get_or_404(BlogPost, post_id)
edit_form = BlogPostForm(
title=post.title,
subtitle=post.subtitle,
img_url=post.img_url,
author_name=post.author,
blog_content=post.body
)
if edit_form.validate_on_submit():
post.subtitle = edit_form.subtitle.data,
post.img_url = edit_form.img_url.data,
post.author = edit_form.author_name.data,
post.body = edit_form.blog_content.data,
db.session.commit()
return redirect(url_for("show_post", post_id=post_id))
return render_template("make-post.html", is_edit=True, form=edit_form)
错误:
sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) Error binding parameter 1: type 'tuple' is not supported
[SQL: UPDATE blog_post SET subtitle=?, body=?, author=?, img_url=? WHERE blog_post.id = ?]
[parameters: (('Lego is awsome',),....
(Background on this error at: https://sqlalche.me/e/20/f405)
为什么?
在您的更新文件中,删除逗号,例如
post.img_url = edit_form.img_url.data;
以下代码
... post.subtitle = edit_form.subtitle.data
post.img_url = edit_form.img_url.data
post.author = edit_form.author_name.data
post.body = edit_form.blog_content.data 和 db.session.commit()
...