def add_persons(request): person = Person() form = PersonForm(request.POST) if request.method == 'POST' and form.validate(): form.populate_obj(person) DBSession.add(person) return HTTPFound(location=request.route_url('index')) return {'form' : form}
def create(): form = PersonForm(request.form) if request.method == 'POST' and form.validate(): person = Person.create( firstname=form.firstname.data, lastname=form.lastname.data, birthday=form.birthday.data, zipcode=form.zipcode.data) if request.is_xhr: return jsonify(success=True, person=person.serialize()) else: return redirect(url_for('index')) # if it's an ajax request and if fails # form validation return message to client if request.is_xhr and request.method == 'POST' and not form.validate(): return jsonify(success=False, errors=form.errors) return render_template('create.html', form=form)
def edit(id): try: person = Person.get(Person.id == id) except Person.DoesNotExist: abort(404) form = PersonForm(request.form, person) if request.method == "POST" and form.validate(): person.firstname = form.firstname.data person.lastname = form.lastname.data person.birthday = form.birthday.data person.zipcode = form.zipcode.data person.save() return redirect(url_for('index')) return render_template('edit.html', form=form, person=person)