O'Reilly logo

Instant Flask Web Development by Ron DuPlain

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Deleting a record (Advanced)

We are all set with our appointment interactions. Wait; how do we delete appointments?

Getting ready

We will continue to work from sched's app.py file and templates directory.

How to do it...

  1. We build an Ajax handler, in app.py:
    @app.route('/appointments/
      <int:appointment_id>/delete/',
      methods=['DELETE'])
    
    def appointment_delete(appointment_id):
      """Delete record using HTTP DELETE, respond with JSON."""
      appt = db.session.query(Appointment).get(appointment_id)
      if appt is None:
        # Abort with Not Found, but with simple JSON response.
        response = jsonify({'status': 'Not Found'})
        response.status = 404
        return response
      db.session.delete(appt)
      db.session.commit()
      return jsonify({'status': 'OK'})
  2. A little jQuery will wire up all delete ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required