【问题标题】:Python Flask - WTF SelectField data causes an error _mysql_exceptions.ProgrammingErrorPython Flask - WTF SelectField 数据导致错误_mysql_exceptions.ProgrammingError
【发布时间】:2018-10-20 21:04:06
【问题描述】:

当我尝试将数据从 SelectField 添加到 MySQL 数据库时,会发生此错误:

_mysql_exceptions.ProgrammingError _mysql_exceptions.ProgrammingError: 在字符串格式化期间并非所有参数都转换了

我的代码

class ContentForm(Form):
category = SelectField("Category: ",choices=[('DataScience','Data Science'), ('ComputerProgramming', 'Computer Programming'),('DigitalMarketing','Digital Marketing'),('Other','Other')], validators=[validators.DataRequired()])
title = StringField("Title: ",validators=[validators.length(min= 5, max = 100), validators.DataRequired()])
content = TextAreaField("Content: ", validators=[validators.length(min = 10), validators.DataRequired()])

@app.route("/addcontent", methods=["GET","POST"])
@login_required
def addcontent():
form = ContentForm(request.form)
if request.method == "POST" and form.validate():
    category = form.category.data
    title = form.title.data
    content = form.content.data
    cursor = mysql.connection.cursor()
    query = "INSERT INTO contents (author, category, title,content) VALUES(%s,%s,%s)"
    cursor.execute(query, (session["username"], category, title, content))
    mysql.connection.commit()
    cursor.close()
    flash("Your content have added successfully. Thanks your contributions.", 'success')
    return redirect(url_for("dashboard"))
return render_template("addcontent.html", form = form)

HTML:

{% from "includes/_formhelpers.html" import render_field %}

<form method="post">

        {{ render_field(form.category, class="form-control", style = "width: 40% !important") }}
        {{ render_field(form.title, class="form-control", style = "width: 40% !important") }}
        {{ render_field(form.content, class="form-control", style = "height: 300px !important") }}

    <button type="submit" class="btn btn-primary btn-lg">Add Content</button>   

我该如何解决这个问题?

【问题讨论】:

    标签: python mysql python-3.x flask flask-wtforms


    【解决方案1】:

    你的问题来自于

    cursor.execute(query, (session["username"], category, title, content))
    

    您应该以身份访问会话

    session.get('username')
    

    尝试使用一些字符串执行查询以进行测试,看看它是否会成功执行,例如:

    cursor.execute(query, 'myName', category, title, content))
    

    【讨论】:

    • 我认为发生错误是因为“类别”(SelectField)而不是“会话”。因为当我像这样删除“类别”时: cursor.execute(query, (session["username"], title, content)) 它工作得很好。
    • 你能发布你的 html 表单,你是如何渲染它的吗?我昨天试过你的例子,我认为唯一的问题是那个会话,因为如果你正确渲染所有其他字段,它将包含数据。
    • 'code' {% from "includes/_formhelpers.html" import render_field %}
      {{ render_field(form.category, class="form-control", style = "宽度:40% !important") }} {{ render_field(form.title, class="form-control", style = "width: 40% !important") }} {{ render_field(form.content, class=" form-control", style = "height: 300px !important") }}
    • 也许您需要在表单上使用 action=""。另外,请在这里查看我的答案link
    猜你喜欢
    • 2012-12-20
    • 2013-05-24
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多