7

Try to create a register page for my app. I am using Flask framework and MySQL db from pythonanywhere.com.

@app.route('/register/', methods=["GET","POST"]) 
def register_page():
try:
    form = RegistrationForm(request.form)



    if request.method == "POST" and form.validate():
        email = form.email.data
        password = sha256_crypt.encrypt((str(form.password.data)))
        c, conn = connection()

        x = c.execute("SELECT * FROM users WHERE email = (%s)",
                      (email))

        if int(x) > 0:
            flash("That email adress is already in use.")
            return render_template('register.html', form=form)

        else:
            c.execute("INSERT INTO users (email, password) VALUES (%s, %s)",
                      (thwart(email),thwart(password)))

            conn.commit()
            flash("Thanks for registering!")
            c.close()
            conn.close()
            gc.collect()

            session['logged_in'] = True
            session['email'] = email

            return redirect(url_for('dashboard'))

    return render_template("sign-up.html", form=form)


except Exception as e:
    return(str(e))}

On running I get the Error:not all arguments converted during string formatting. How to fix it? May be the problem in this statement?

c.execute("INSERT INTO users (email, password) VALUES (%s, %s)", (thwart(email),thwart(password)))

3
  • 1
    Are you 100% sure that the error is happening at that line? I think the problem is more likely to be at the x = c.execute() line above, where you should have (email,) -- note the trailing comma -- as the last parameter, not (email) with no trailing comma. Commented Oct 28, 2015 at 18:01
  • I changed (email) to (email,) and got the same result Commented Oct 28, 2015 at 18:08
  • 1
    It worked! I just didn't restart the server. Thank You! Commented Oct 28, 2015 at 18:18

1 Answer 1

8

Just converting my earlier comment to an answer, as it seemed to be the right solution :-)

The problem is coming from a different line. You have this:

        x = c.execute("SELECT * FROM users WHERE email = (%s)",
                      (email))

This doesn't do what you might think it does. Putting email in brackets does nothing, so the line is actually equivalent to passing in each character of whatever's in that variable in a list of characters. If instead you do this:

        x = c.execute("SELECT * FROM users WHERE email = (%s)",
                      (email,))

...then you'll be passing in a tuple containing one item, email, and it should work better.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.