Question

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)
Was it helpful?

Solution

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.

OTHER TIPS

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).

Did you remember to import request? Not sure what you're using but it looks like Flask to me, and if so it's 'from flask import request'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top