当然应该有一种更简洁的方式来编写这个 Flask 路由代码

Surely there Should be a more clean way of writing this Flask route code

我想在我的网络页脚部分编辑我的联系人,我已经这样做了: 在我的管理页面中,我做了它,这样我就可以为每个联系人填写一个包含 4 个输入的表单 form.contact1 form.contact2 form.contact3 form.contact4... 同时我制作了一个包含 content 列的 Contacts 模型。


我的路线代码:

@app.route('/admin/media-kontaktai', methods=['GET', 'POST'])
@login_required
def media_contacts():
    form = MediaContactsForm()

    if form.validate_on_submit():
        rows = db.session.query(func.count(Contacts.id)).scalar()
        for x in range(1, rows+1):
            erace_contact = Contacts.query.get(x)
            db.session.delete(erace_contact)
            db.session.commit()
        if form.contact1.data:
            contact = Contacts(content=form.contact1.data)
            db.session.add(contact)
            db.session.commit()
        if form.contact2.data:
            contact = Contacts(content=form.contact2.data)
            db.session.add(contact)
            db.session.commit()
        if form.contact3.data:
            contact = Contacts(content=form.contact3.data)
            db.session.add(contact)
            db.session.commit()
        if form.contact4.data:
            contact = Contacts(content=form.contact4.data)
            db.session.add(contact)
            db.session.commit()
        flash('Informacija atnaujinta!', 'success')
        return redirect(url_for('media_contacts'))
    elif request.method == 'GET':
        contact1 = Contacts.query.get(1)
        form.contact1.data = contact1.content
        contact2 = Contacts.query.get(2)
        form.contact2.data = contact2.content
        contact3 = Contacts.query.get(3)
        form.contact3.data = contact3.content
        contact4 = Contacts.query.get(4)
        form.contact4.data = contact4.content
    contacts = Contacts.query.all()
    return  render_template('a-media-kontaktai.html', title='Media ir kontaktai', form=form, contacts=contacts)

这是未经测试的。看看是否有效:

for i in range (1,5):
    if form['contact' + str(i)].data:
        contact = Contacts(content=form['contact' + str(i)].data)
        db.session.add(contact)
        db.session.commit()

我还没有对此进行测试,但它应该可以工作,或者至少让您走上正确的道路,了解如何进一步压缩它。

@app.route('/admin/media-kontaktai', methods=['GET', 'POST'])
@login_required
def media_contacts():
    form = MediaContactsForm()

    if request.method == 'POST':
        if form.validate_on_submit():
            try:
                # You can just delete all records in one go instead of 
                # wasting time iterating and checking lengths
                db.session.query(Contacts).delete()
                db.session.commit()
            except:
                db.session.rollback()
            
            # I am iterating fields in the form that have data 
            # so we don't need multiple if statements
            for field in [field for field in form if field.data]:
                contact = Contacts(content=field.data)
                db.session.add(contact)
                db.session.commit()
            
            flash('Informacija atnaujinta!', 'success')
            return redirect(url_for('media_contacts'))
    
    else:
        for field, index in enumerate(form):
            for contact in Contacts.query.all():
                field.data = contact.content
    
    contacts = Contacts.query.all()
    return  render_template('a-media-kontaktai.html', title='Media ir kontaktai', form=form, contacts=contacts)