简体   繁体   中英

Exact match with filter() Flask

I'am new to python and iam coding a small webapp to search in a database. Its fonctionnal but doesnt function well.

Example : If i search for a 'lieu', a 'Annee' and a 'Signataire' it doesnt give me the exact matches, instead it gives me every 'Signataire' even if its not the 'Annee' or the 'lieu' that i was looking for.

Update : I changed to elif, it helps finding the right "signataire" but still gives me others "signataire" that comes with 'lieu' and 'annee' iam a bit lost.

Here is the code :

'''

def search_results(search):

    results = []
    lieu = search.data['Lieu']
    annee = search.data['Annee']
    signataire = search.data['Signataire']
    cote = search.data['Cote']


    if search.data['Lieu'] or search.data['Annee'] or 
    search.data['Cote']:
        qry = db_session.query(Cote).filter(and_(Cote.lieu.contains(lieu),Cote.annee.contains(annee),Cote.cote.contains(cote))
    )
        



    results = qry.all()

    elif search.data['Signataire']:
        qry = db_session.query(Cote).filter(or_(Cote.signataire_nom.contains(signataire),Cote.signataire_titre.contains(signataire)))
   
        results = qry.all()

    if not results:
        flash('No results !')
        return redirect('/')

    else:
        table = Results(results)
        table.border = True
        return render_template('results.html', table=table)

'''

This is because you are using "if" the second time and not "elif". So if there exists the Signataire, then your results are always all the signataire. To resolve this you should use something like this:

def search_results(search):

results = []
lieu = search.data['Lieu']
annee = search.data['Annee']
signataire = search.data['Signataire']
cote = search.data['Cote']


if search.data['Lieu'] or search.data['Annee'] or search.data['Cote']:
    qry = db_session.query(Cote).filter(and_(
        Cote.lieu.contains(lieu),Cote.annee.contains(annee),Cote.cote.contains(cote))
    )

    results = qry.all()

elif search.data['Signataire']:
    qry = db_session.query(Cote).filter(or_(
        Cote.signataire_nom.contains(signataire),Cote.signataire_titre.contains(signataire)))

    results = qry.all()

if not results:
    flash('No results !')
    return redirect('/')

else:
    table = Results(results)
    table.border = True
    return render_template('results.html', table=table)

Please add the indent to all the other lines except the define function line**

It helps but iam still struggling. With this code if i search for an exact match of signataire, lieu, annee it gives me only lieu, annee and seems to ignore signataire =/ but the condition is there , iam lost =D

Update : If i search only for signataire it gives me the exact match but i want it to take all the condition in consideration.

I found a way to solve this. Thank you for your collaboration i appreciate.

Jaybe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM