views:

108

answers:

3

Basically I want to return the contents of create_user in the register function to use to save to my database. I am a complete beginner. What am I misunderstanding?

def register():
    form = SignupForm(request.form)
    if request.method == 'POST' and form.validate():
    create_user = ({'username' : form.username.data, 'email' : form.email.data,
                        'password': form.password.data})
    flash('Thanks for registering')
    return create_user, redirect(url_for('loggedin.html'))
return render_template('get-started.html', form=form)

create_user = register()
doc_id, doc_rev = db.save(create_user)
+2  A: 

Your indenting is wrong; you want:

def register():
    form = SignupForm(request.form)
    if request.method == 'POST' and form.validate():
        create_user = ({'username' : form.username.data, 'email' : form.email.data,
                            'password': form.password.data})
        flash('Thanks for registering')
        return create_user, redirect(url_for('loggedin.html'))
    return render_template('get-started.html', form=form)

Indenting delineates blocks of code. You need to indent everything inside the function to show that it is the code corresponding with that function, and everything inside the if. You haven't indented for the if.

katrielalex
+4  A: 

I think you've lost some formatting somewhere. The first return statement should be indented far enough that it's inside the if block and the second return statement should line up with the if block. If validation succeeds then it returns the tuple create_user, redirect(url_for('loggedin.html')), otherwise it returns render_template('get-started.html', form=form).

g.d.d.c
A: 
@app.route('/get-started/', methods=['GET', 'POST'])
def register():
    form = SignupForm(request.form)
    if request.method == 'POST' and form.validate():
        create_user = ({'username' : form.username.data, 'email' : form.email.data,
                        'password': form.password.data})
        flash('Thanks for registering')
        return create_user, redirect(url_for('loggedin.html'))
    return render_template('get-started.html', form=form)

create_user = register()    
doc_id = db.save(create_user)

Okay, I've indented properly but the program won't run: says 'AttributeError: 'NoneType' object has no attribute 'request'

I'm just not getting this...?

Handloomweaver