M
M
Muriam2021-11-10 16:38:34
Flask
Muriam, 2021-11-10 16:38:34

Flask. How to search on a form?

How to do a search in the database when entering a word in a text field and clicking on a button?

If I enter the word "Common viburnum", then as a result the following message should appear on the form:

Common viburnum -> Viburnum -> Adox

Here "Common viburnum" is the name field in the species table; "Kalina" is a name in the genus table; "adox" is the name in the family table.

...

class Family(db.Model):
    __tablename__ = 'family'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(40))
    with_species = db.relationship('Species', backref='family')

class Genus(db.Model):
    __tablename__ = 'genus'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(40))
    with_species = db.relationship('Species', backref='genus')

class Species(db.Model):
    __tablename__ = 'species'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(40))
    genus_id = db.Column(db.Integer, db.ForeignKey('genus.id'))
    family_id = db.Column(db.Integer, db.ForeignKey('family.id'))



class SpeciesForm(FlaskForm):
    find = StringField('название')
    findButton = SubmitField('найти')


@app.route('/species', methods=['GET', 'POST'])
def species():
    form = SpeciesForm()
    if form.validate_on_submit():
       ...
        return render_template('species.html', species=species, form=form)
    return render_template('species.html', form=form)


{% extends "index.html" %}

{% block content %}
<div id="content">

<form action="" method="POST">

    {{ form.hidden_tag() }}

<div class="items">

    <div class="item">
        <i>Поиск</i>
        <br><br>
        {{ form.find.label }}  {{ form.find() }}
        <br><br>
        {{ form.findButton }}
    </div>

</div>
</form>

</div>
{% endblock %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ramis, 2021-11-11
@Muriam

class SpeciesForm(FlaskForm):
    find = StringField('название', validators=[DataRequired(message='Обязательное поле')])
    findButton = SubmitField('найти')


@app.route('/species', methods=['GET', 'POST'])
def species():
    form = SpeciesForm()
    if form.validate_on_submit():
        species_search = Species.query.filter(Species.name.like(f'%{form.find.data}%')).all()
        return render_template('species.html', species_search=species_search, form=form)
    return render_template('species.html', form=form)

{% for species in species_search %}
    <p>{{ species.name }} -> {{ species.genus.name }} -> {{ species.family.name }}</p> 
{% endfor %}
    <div id="content">
        <form action="" method="POST">
        
            {{ form.hidden_tag() }}
        
        <div class="items">
        
            <div class="item">
                <i>Поиск</i>
                <br><br>
                {{ form.find.label }}  {{ form.find() }}
                <br><br>
                {{ form.findButton }}
            </div>
        
        </div>
        </form>
        </div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question