-3

I want to access couple of variables from if-block, outside in the function

eg: i want to use signin, user

@app.route('/signup', methods=['GET', 'POST'])
def register():
    form1 = Signup()
    if form1.validate_on_submit():
        signin = auth_py.create_user_with_email_and_password(form1.email_address.data, form1.password.data)
        user = auth_py.send_email_verification(signin['idToken'])  # for email verification

        name = form1.name.data
        username = form1.username.data
        email_address = form1.email_address.data

        return redirect(url_for('home_page'))
    if form1.errors != {}:   # if no errors occur from validators
        for err_msg in form1.errors.values():
            print(f'there was an error creating the user {err_msg}')
    database(username, name, email_address) # problem is here
    return render_template('sign-up.html', form1=form1)

I want to use name, username and email_address from form1.validate_on_submit() inside database() and i don't know how can i do that.

NOTE: there is some code yet to be written in main program so I cannot call database() function inside form1.validate_on_submit()

vandit vasa
  • 413
  • 4
  • 20
vandit
  • 31
  • 1
  • 5

1 Answers1

1
signin = None;
user   = None;    
    def register():
        form1 = Signup()
        if form1.validate_on_submit():
            signin = auth_py.create_user_with_email_and_password(form1.email_address.data, form1.password.data)
            user = auth_py.send_email_verification(signin['idToken'])  # for email verification
    
            name = form1.name.data
            username = form1.username.data
            email_address = form1.email_address.data
    
            return redirect(url_for('home_page'))
        if form1.errors != {}:`enter code here`

Define variable outside your if-block, this way you can use the variables outside the if-block.

Groot
  • 171
  • 13
  • but I want values that are stored inside that variable from that if-statement – vandit vasa Apr 07 '21 at 02:38
  • why you think the value you're assigning in if-block won't be available after execution of if block. the scope of 'signin' & 'user' is outside the if block, it will retain the value you want to assign in the if-block. – Groot Apr 07 '21 at 02:47
  • @vanditvasa The `if` block returns unconditionally. You'll never call your `database` function at all if the `if` condition is true. – jamesdlin Apr 07 '21 at 02:53
  • please up-vote if it helps. @vanditvasa – Groot Apr 07 '21 at 03:03