def edit_twit(): user_id = current_user.user_id form = editTwitForm() if request.args.get('id'): twit_id = request.args.get('id') # again, we don't use sql to get the twit # but instead we use the Twits class # and the query attribute provided by flask-sqlalchemy # see also # http://flask-sqlalchemy.pocoo.org/2.3/queries/#querying-records twit = Twits.query.filter_by(twit_id = twit_id).first() # note that the query object is over all records # to get the specific twit we use the first() methods # having obtained the twits object we can access the text as # the attribute twit.twit form.twit.data = twit.twit form.twit_id.data = twit_id return render_template('edit_twit_mysql.html',form=form) if form.validate_on_submit(): # get the twit_id back from the form twit_id = form.twit_id.data # load that twit twit = Twits.query.filter_by(twit_id = twit_id).first() # change the twit text to the text submitted in the form twit.twit = form.twit.data # commit the change db.session.commit() # so in summary, the way to update db entries is # 1. retrieve the object you want to change # 2. change the attribute # 3. commit the session # see also # https://stackoverflow.com/questions/6699360/flask-sqlalchemy-update-a-rows-information#6701188 return redirect(vs_url_for('index')) return render_template('edit_twit_mysql.html',form=form)
def edit_twit(): user_id = current_user.user_id form = editTwitForm() if request.args.get('id'): twit_id = request.args.get('id') twit = Twits.query.filter_by(twit_id=twit_id).first() form.twit.data = twit.twit form.twit_id.data = twit_id print(form.twit.data) print(form.twit_id.data) return render_template('edit_twit_mysql.html', form=form) if form.validate_on_submit(): twit_id = form.twit_id.data # load the twit twit = Twits.query.filter_by(twit_id=twit_id).first() usr = twit.user_id if (user_id != usr): abort(401) else: #twit = Twits.query.filter_by(twit_id = twit_id).first() # change the twit text to the text submitted in the form twit.twit = form.twit.data print(twit.twit) db.session.commit() return redirect(vs_url_for('index')) return render_template('edit_twit_mysql.html', form=form)
def edit_twit(): user_id = current_user.user_id form = editTwitForm() if request.args.get('id'): twit_id = request.args.get('id') # we use the Twits class and the query attribute provided by flask-sqlalchemy twit = Twits.query.filter_by(twit_id=twit_id).first() # the query object is over all records # to get the specific twit we use the first() methods # having obtained the twits object we can access the text as # the attribute twit.twit form.twit.data = twit.twit form.twit_id.data = twit_id return render_template('edit_twit_mysql.html', form=form) if form.validate_on_submit(): # get the twit_id back from the form twit_id = form.twit_id.data # load that twit twit = Twits.query.filter_by(twit_id=twit_id).first() # change the twit text to the text submitted in the form twit.twit = form.twit.data # commit the change db.session.commit() return redirect(vs_url_for('index')) return render_template('edit_twit_mysql.html', form=form)
def edit_twit(): form = editTwitForm() if request.args.get('id'): twit_id = request.args.get('id') twit = db.get_twit(twit_id) form.twit.data = twit['twit'] form.twit_id.data = twit_id return render_template('edit_twit_mysql.html', form=form, twit=twit) if form.validate_on_submit(): twit = form.twit.data twit_id = form.twit_id.data db.update_twit(twit, twit_id) return redirect(vs_url_for('.index')) return render_template('edit_twit_mysql.html', form=form)
def edit_twit(): user_id = current_user.user_id form = editTwitForm() if request.args.get('id'): twit_id = request.args.get('id') twit = Twits.query.filer_by(twit_id=twit_id).first() form.twit.data = twit.twit form.twit_id.data = twit_id return render_template('edits_twits.html', form=form, twit=twit) if form.validate_on_submit(): twit_id = form.twit_id.data twit = Twits.query.filter_by(twit_id=twit_id).first() twit.twit = form.twit.data db.session.commit() return redirect(vs_url_for('index')) return render_template('edits_twits.html', form=form)
def edit_twit(): user_id = current_user.user_id form = editTwitForm() if request.args.get('id'): twit_id = request.args.get('id') twit = Twits.query.filter_by(twit_id = twit_id).first() form.twit.data = twit.twit form.twit_id.data = twit_id return render_template('edit_twit_mysql.html',form=form) if form.validate_on_submit(): # get twit_id back from form twit_id = form.twit_id.data # load twit twit = Twits.query.filter_by(twit_id = twit_id).first() # change twit text to text submitted in form twit.twit = form.twit.data # commit change db.session.commit() return redirect(vs_url_for('index')) return render_template('edit_twit_mysql.html',form=form)