[Flask] if/else - form handling code

Corey Boyle coreybrett at gmail.com
Mon Sep 9 09:40:38 EDT 2019


I have this in one of my views...

        if form.validate_on_submit():
            ir.notes = form.notes.data
            ir.direction = form.direction.data
            ir.insp_date = form.insp_date.data
            ir.in_out_date = form.in_out_date.data
            ir.customer_pk = form.customer.data
            ir.branch_pk = form.branch.data
            db.session.add(ir)
            db.session.commit()
            return redirect(url_for('sales.inspectionreport', irpk=ir.pk))
        else:
            form.notes.data = ir.notes
            form.direction.data = ir.direction
            form.insp_date.data = ir.insp_date
            form.in_out_date.data = ir.in_out_date
            form.customer.data = ir.customer_pk
            form.branch.data = ir.branch_pk

The else does all the same assignments as the if, but in reverse order.

Is there a better way to write this?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20190909/f0263c0f/attachment.html>


More information about the Flask mailing list