O
O
osipyanarsen2021-06-27 20:25:14
Flask
osipyanarsen, 2021-06-27 20:25:14

Why is the form associated with the database not updated during the execution of a Flask web application?

The site has the ability to add coffee varieties, as well as recipes for each variety (in the recipe creation form, you need to specify the type of coffee that you need to prepare). The problem is that if you start the web application with the command flask run, create a new variety, and then try to create a recipe, then the new variety will not appear in the list of varieties, and it will appear only after the server is restarted.

This is the code of the page with the form for adding a new variety:

@app.route('/new_sort', methods=['GET', 'POST'])
@login_required
def new_sort():
    if current_user.email not in app.config["ADMINS"]:
        return redirect(url_for('index'))
    form = SortForm()
    if form.validate_on_submit():
        sort = Sort(title=form.title.data,
                    user_id=current_user.id,
                    bouquet=form.bouquet.data,
                    description=form.description.data)
        db.session.add(sort)
        db.session.commit()
        flash('Новый сорт добавлен!')
        return redirect(url_for('index'))
    return render_template('sort_form.html', title='Новый сорт', form=form, ADMIN_EMAILS=Config.ADMINS)


Similarly for the recipe:
@app.route('/new_recipe', methods=['GET', 'POST'])
@login_required
def new_recipe():
    form = RecipeForm()
    if form.validate_on_submit():
        recipe = Recipe(title=form.title.data,
                        user_id=current_user.id,
                        sort_id=Sort.query.filter_by(title=str(form.sort_id.data)).first().id,
                        coffee_mass=form.coffee_mass.data,
                        water_mass=form.water_mass.data,
                        water_temp=form.water_temp.data,
                        grinding=form.grinding.data,
                        acidity=form.acidity.data,
                        tds=form.tds.data,
                        body=form.body.data)
        db.session.add(recipe)
        db.session.commit()
        flash('Новый рецепт добавлен!')
        return redirect(url_for('index'))
    return render_template('recipe_form.html', title='Новый рецепт', form=form, ADMIN_EMAILS=Config.ADMINS)


And here is the class RecipeForm( sort_id- part of the form that is not updated during server operation):
class RecipeForm(FlaskForm):
    title = StringField('Название рецепта:', validators=[DataRequired(EMPTY_FIELD)])
    sort_id = SelectField("Выберите название кофе:", validators=[DataRequired(EMPTY_FIELD)],
                          choices=Sort.query.all())
    coffee_mass = DecimalField("Масса кофе, г:", validators=[DataRequired(EMPTY_FIELD)])
    water_mass = IntegerField("Масса воды, мл:", validators=[DataRequired(EMPTY_FIELD)])
    water_temp = IntegerField("Температура воды, C:", validators=[DataRequired(EMPTY_FIELD)])
    acidity = IntegerField("Кислотность (оцените сами от 0 до 10):")
    tds = IntegerField("Насыщенность (оцените сами от 0 до 10):")
    grinding = DecimalField("Помол:", validators=[DataRequired(EMPTY_FIELD)])
    body = TextAreaField("Шаги", validators=[DataRequired(EMPTY_FIELD)])
    submit = SubmitField("Добавить"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
medvedodesa, 2021-07-01
@medvedodesa

use QuerySelectedField
and after add :
return redirect(request.referrer)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question