Python flask代码优化求大神指点,pythonflask,@bp.route(/s


@bp.route('/settings/<username>', methods=['GET', 'POST'])@login_requireddef 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

直接上代码:

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

编橙之家文章,

评论关闭