Answer the question
In order to leave comments, you need to log in
Python, Flask. How to fill forms with data from the database?
Good evening.
I'm trying to make a page for editing the properties of a certain sensor (s).
Tell me how you can fill in the forms with existing data in the database?
Now the code looks like this:
forms.py
class EditSensorForm(FlaskForm):
w1_addr = StringField('1-wire адрес', validators=[DataRequired()])
pin = StringField('Пин')
function = StringField('Функция', validators=[DataRequired()])
place = StringField('Место', validators=[DataRequired()])
submit = SubmitField('Подтвердить')
@bp.route('/settings/sensors/edit/<sensor_id>', methods=['GET', 'POST'])
@login_required
def edit_sensor(sensor_id):
sensor = Sensor.query.filter_by(id=sensor_id).first_or_404()
form = EditSensorForm()
if form.validate_on_submit():
sensor.w1_addr = form.w1_addr.data
sensor.GPIO_pin = form.pin.data
sensor.function = form.function.data
sensor.place = form.place.data
db.session.commit()
flash('Your changes have been saved.')
return redirect(url_for('settings.sensors'))
elif request.method == 'GET':
form.w1_addr.data = sensor.w1_addr
form.pin.data = sensor.GPIO_pin
form.function.data = sensor.function
form.place.data = sensor.place
return render_template('/settings/edit_sensor.html',
form=form,
title='Редактирование свойств датчика')
{{ wtf.quick_form(form) }}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question