3

Just starting out with Flask and Python. I have a simple form that does an ajax post using the jQuery ajax form library. It goes to a function on the python side and add's a user to a mongoDb database. It then returns a true or false.

Upon returning the boolean I get this error:

TypeError: 'bool' object is not iterable

Most recent traceback:

File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__

return self.wsgi_app(environ, start_response)

Python side

@app.route('/signup', methods=['GET', 'POST'])
def signup():
    if request.method == 'GET':
        return render_template('signup.html')
    if request.method == 'POST':
        result = create_user(request.form["firstname"],
                    request.form["lastname"],
                    request.form["username"],
                    request.form["password"],
                    request.form["email"])
        return jsonify(result)

Javascript

<script type="application/javascript">
    $(document).ready(function() {
            // bind 'myForm' and provide a simple callback function
            $('#signupform').ajaxForm(function() {
                alert("Thank you for your comment!");
            });
        });
</script>

Create_User Function

def create_user(form_first_name, form_last_name, form_username, form_password, form_email):
    user = User()
    user.first_name = form_first_name
    user.last_name = form_last_name
    user.last_modified = datetime.now()
    user.username = form_username
    user.password = form_password
    user.email = form_email
    if user.save():
        return True
    else:
        return False
Will
  • 361
  • 7
  • 17
  • What does create_user do ? Can you show the code for that function ? I am guessing that create_user is returning some kind of True/False which cannot work with jsonify. You must supply a dictionary to jsonify and create_user should return a dictionary. – codegeek Mar 13 '14 at 14:04

1 Answers1

0

Try returning:

return jsonify(result=result)

Check out the docs: http://flask.pocoo.org/docs/api/#flask.json.jsonify.

I suspect create_user() is returning a bool type which has no dictionary representation.

Nick Rempel
  • 3,022
  • 2
  • 23
  • 31