def home(): form = UserForm(request.form) if request.method == 'POST' and form.validate(): user = User(username=form.data.get('username')) form.populate_obj(user) try: ss.add(db.session, user) except IntegrityError: db.session.rollback() form.username.errors.append('Username already exists!') else: return redirect(url_for('show_users')) return render_template('welcome.html', form=form)
def add_user(username): user = User(username=username) try: user = ss.add(db.session, user) except IntegrityError: # Always issue a rollback when a transaction fails db.session.rollback() return 'Username {} already exists!'.format(username) return 'Hello world, {}'.format(user.username)