Can't understand, why this code doesn't work, i was trying to understand, but i'm only learning Flask&Py, help me please:
Flask:
@freepylib.route('/register', methods=['POST', 'GET'])
def signup_user():
if request.method == "POST":
username, useremail, userpasswd=request.form['name'], request.form['email'], request.form['password']
reg = Users(uname=username, useremail=useremail, upasswrd=userpasswd)
try:
mydb.session.add(reg)
mydb.session.commit()
token = reg.get_token()
return {'access_token': token}
except Exception as e:
return str(e)
else:
return render_template("register.html")
HTML code:
<form method="POST" name="reg" id="reg">
<input type ="text" class ="fadeIn second" name ="name" placeholder="name">
<input type="text" name="email" placeholder="email">
<input type="text" name="password" placeholder="password">
<input type="submit" name="subm" value="Sign Up">
</form>
Error:
(pymysql.err.IntegrityError) (1048, "Column 'uname' cannot be null") [SQL: INSERT INTO `PFL_USERS` (uname, email, upasswrd) VALUES (%(uname)s, %(email)s, %(upasswrd)s)] [parameters: {'uname': None, 'email': None, 'upasswrd': None}] (Background on this error at: http://sqlalche.me/e/13/gkpj)
CodePudding user response:
I think the problem is in your Users class, in the __init__. The kwargs need to match up with how you're calling the constructor from your app code.
For example, changing it to this should help:
def __init__(self, **kwargs):
self.uname = kwargs.get('uname')
self.email = kwargs.get('useremail')
self.upasswrd = kwargs.get('upasswrd')
CodePudding user response:
Special thanks to @ChrisSears, you make my day
Problem was in class, which i forget to add(i'm really sorry)
So, i'll describe:
reg = Users(uname=username, email=useremail, upasswrd=hash_password(userpasswd))
Using this, i've transfer data into Class Users:
def __init__(self, **kwargs):
self.uname = kwargs.get('name')
self.email = kwargs.get('email')
self.upasswrd = kwargs.get('password')
As we can see, name of variables is not correct, so there is the problem, it should be like this:
def __init__(self, **kwargs):
self.uname = kwargs.get('uname')
self.email = kwargs.get('email')
self.upasswrd = kwargs.get('upasswrd')
