python - 'NoneType' object has no attribute 'username'? -
so got error
attributeerror: 'nonetype' object has no attribute 'username'
any suggestions,
models.py
class user(db.model): __tablename__ = "users" id = db.column(db.integer, primary_key=true) username = db.column(db.string(80), unique=true) password = db.column(db.string(20), unique=true) def __init__(self, username, password): self.username = username self.password= bcrypt.generate_password_hash(password, 10) def is_authenticated(self): return true def is_active(self): return true def is_anonymous(self): return false def get_id(self): return (self.id) def __repr__(self): return '<user %r>' % self.username
views.py
@app.route('/register', methods=['get','post']) def register(): if request.method == 'get': return render_template('register.html') user = user(request.form['username'] , request.form['password'] ) username = request.form['username'] password = request.form['password'] storeduser = user.query.filter_by(username=username).first() if storeduser.username not none , storeduser.username == request.form['username']: return 'user exist !' else: db.session.add(user) db.session.commit() flash('user registered') return redirect(url_for('index'))
pretty if client username matches current username output existing user registered, if not register username.
im referring username in models(in case wondering)
the user.query.filter_by(username=username).first()
query returned none
, meaning user not found.
you need test if storeduser
not none
first, not storeduser.username
:
if storeduser not none , storeduser.username == request.form['username']: return 'user exist !'
because queried on request.form['username']
latter test redundant, need use;
if storeduser not none: return 'user exist !'
Comments
Post a Comment