首页 > 一段python(flask)代码,可否写的更优雅或简洁一点?

一段python(flask)代码,可否写的更优雅或简洁一点?

@bp.route('/settings/<username>', methods=['GET', 'POST'])
@login_required
def settings():
    profile = User.query.get_or_404(username)
    form = SettingsForm()
    if form.validate_on_submit():
        profile.email = form.email.data
        profile.location = form.location.data
        profile.website = form.website.data
        profile.bio = form.bio.data
        profile.twitter = form.twitter.data
        profile.weibo = form.weibo.data
        profile.github = form.github.data
        profile.instagram = form.instagram.data

        db.session.add(profile)
        db.session.commit()

        return redirect(url_for('main.index'))

就是那几行profile.xxx = form.xxx.data


for k in form.__dict__:
  profile.__dict__[k] = form.__dict__[k].data

直接上代码:

form.populate_obj(profile)
【热门文章】
【热门文章】