Python Flask应用程序:TypeError:'NoneType'对象不可订阅

时间:2017-11-21 18:04:30

标签: python flask nonetype

当我运行我的代码时,会出现错误 它在我的主文件中,我实际上知道错误来自哪里 没有计划如何解决它。

以下是代码:

# Edit Article
@app.route('/article_edit/<string:id>', methods=['GET', 'POST'])
@is_logged_in
def article_edit(id):
    # Create cursor
    cur = mysql.connection.cursor()

    # Get article by id
    result = cur.execute("SELECT * FROM articles WHERE id = %s", [id])

    article = cur.fetchone()
    cur.close()
    # Get form
    form = ArticleForm(request.form)

    # Populate article form fields
    form.title.data = article['title']
    form.body.data = article['body']

    if request.method == 'POST' and form.validate():
        title = request.form['title']
        body = request.form['body']

        # Create Cursor
        cur = mysql.connection.cursor()
        app.logger.info(title)
        # Execute
        cur.execute ("UPDATE articles SET title=%s, body=%s WHERE id=%s",(title, body, id))
        # Commit to DB
        mysql.connection.commit()

        #Close connection
        cur.close()

        flash('Article Updated', 'success')

        return redirect(url_for('dashboard'))

    return render_template('edit_article.html', form=form)

它来自于form.title.data = article['title']下面的一行,身体无法正常工作。

它吐出的错误就是这个:

  

文件“F:\ Coding \ first_Flask \ app.py”,第245行,在article_edit中       form.title.data = article ['title'] TypeError:'NoneType'对象不可订阅

我愿意接受任何建议

0 个答案:

没有答案