Flask, not all arguments converted during string formatting

2024/10/14 15:31:25

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.datapassword = 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'] = Truesession['email'] = emailreturn 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)))

Answer

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.

https://en.xdnf.cn/q/69398.html

Related Q&A

No module named objc

Im trying to use cocoa-python with Xcode but it always calls up the error:Traceback (most recent call last):File "main.py", line 10, in <module>import objc ImportError: No module named …

Incompatible types in assignment (expression has type List[nothing], variable has type (...)

Consider the following self-contained example:from typing import List, UnionT_BENCODED_LIST = Union[List[bytes], List[List[bytes]]] ret: T_BENCODED_LIST = []When I test it with mypy, I get the followin…

How to convert XComArg to string values in Airflow 2.x?

Code: from airflow.models import BaseOperator from airflow.utils.decorators import apply_defaults from airflow.providers.google.cloud.hooks.gcs import GCSHookclass GCSUploadOperator(BaseOperator):@appl…

Python dryscrape scrape page with cookies

I wanna get some data from site, which requires loggin in. I log in by requestsurl = "http://example.com" response = requests.get(url, {"email":"[email protected]", "…

Python retry using the tenacity module

Im having having difficulty getting the tenacity library to work as expected. The retry in the following test doesnt trigger at all. I would expect a retry every 5 seconds and for the log file to refle…

How to write own logging methods for own logging levels

Hi I would like to extend my logger (taken by logging.getLogger("rrcheck")) with my own methods like: def warnpfx(...):How to do it best? My original wish is to have a root logger writing …

How to use pandas tz_convert to convert to multiple different time zones

I have some data as shown below with hour in UTC. I want to create a new column named local_hour based on time_zone. How can I do that? It seems like pandas tz_convert does not allow a column or panda…

virtualenv, python and subversion

Im trying to use the python subversion SWIG libraries in a virtualenv --no-site-packages environment. How can I make this work?

Float to Fraction conversion in Python

While doing exercise on the topic of float type to Fraction type conversion in Python 3.52, I found the difference between the two different ways of conversion.The first method is:>>> from fra…

How to update an SVM model with new data

I have two data set with different size.1) Data set 1 is with high dimensions 4500 samples (sketches).2) Data set 2 is with low dimension 1000 samples (real data). I suppose that "both data set ha…