def destroy(id=1): try: sections = Section() section = sections.query.get_or_404(id) if section.children: # html or Json response if request_wants_json(): return jsonify(data={ message: "Unauthorized : Must delete child's section' first" }), 422, { 'Content-Type': 'application/json' } else: flash("Unauthorized : Must delete child's section first.", category="danger") return redirect(url_for('sections_page.index')) else: sections.destroy_data(section.id) # html or Json response if request_wants_json(): return jsonify(data={ message: "Record deleted successfully.", section: m_section }) else: flash("Record deleted successfully.", category="success") return redirect(url_for('sections_page.index')) except Exception, ex: print("------------ ERROR ------------\n" + str(ex.message)) flash(str(ex.message), category="warning") abort(404)