获取'NoneType'对象的Flask没有属性'__getitem__'

时间:2015-08-10 19:31:40

标签: javascript python ajax json flask

我正在学习Flask框架,目前我正在尝试将表单的输入值发送到我的数据库。这是我的javascript:

function grabValue() {
    var people = {};
    for (i=0; i<number; i++) {
        var name = document.getElementsByClassName("dynamic")[i].value;
        var propertyName = "Person" + String(i);
        people[propertyName] = String(name);
    }

    console.log(people);

    $.ajax ({
        type: "POST",
        url: "/create",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: JSON.stringify(people),
        success: function(result) {
            console.log(result);
        }
    })
}

这是我各自的查看功能:

@app.route('/create', methods=['GET', 'POST'])
@login_required
def create():
    form = GroupForm()
    req_json = request.get_json()
    print req_json
    if request.method == 'POST' and form.validate():
        group = Group(group_name=form.name.data, numppl=form.numppl.data, author=g.user)
        #db.session.add(group)
        for i in xrange(int(request.form['numppl'])):
            active = Active(active_name=req_json['Person%d' %(i)], points=0)
            #db.session.add(active)
        #db.session.commit()
        flash('Group created called "%s"' %(form.name.data))
        return redirect('/viewgroups')
    return render_template('create.html', title="Create Group", form=form)

我不断收到“TypeError:'NoneType'对象没有属性' getitem '”错误,当打印出req_json并跟踪错误跟踪时,我看到:

*Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
*Restarting with stat
127.0.0.1 - - [10/Aug/2015 12:17:42] "GET / HTTP/1.1" 200 -
None
(All these lines happen when I click the button to submit)
127.0.0.1 - - [10/Aug/2015 12:17:47] "GET /create HTTP/1.1" 200 -
{u'Person2': u'mary', u'Person0': u'bob', u'Person1': u'joe'} <-- Printing properly! 
127.0.0.1 - - [10/Aug/2015 12:17:59] "POST /create HTTP/1.1" 200 -
None <-- becomes none again
Person0
127.0.0.1 - - [10/Aug/2015 12:17:59] "POST /create HTTP/1.1" 500 -
Traceback (most recent call last): (took out callbacks for brevity)
    active = Active(active_name=req_json['Person%d' %(i)], points=0)
TypeError: 'NoneType' object has no attribute '__getitem__'

有人可以向我解释这里发生了什么以及如何解决这个问题?我是Flask和AJAX / JSON的新手,所以我也想了解这个过程。

谢谢!

0 个答案:

没有答案