0

I have created a small app in python flask and in the html file,When i want to search a particular field it searches only after the case matches,ie if i want to search INDIA, it will only show the result when the case matches but when I search india or India it doesn't get searched? what could be the reason for not showing up the result? I later found out from a stackoverflow answer that i need to convert it to uppercase first heres the answer How to let Python recognize both lower and uppercase input?

but how do include it over here?

<input type="text" class="form-control" id="search" name="search" placeholder="search"
                                       value="{{ request.form['search'] }}"></td>

in my py file its if request.method == 'POST': search = request.form['search'].casefold() how would something in request.form['search'] get converted to either .casefold() or .upper() or .lower(). Please help me.

Mayureshk
  • 11
  • 6
  • I hope you will find answer [here](https://stackoverflow.com/questions/23195321/how-to-convert-string-to-uppercase-lowercase-in-jinja2). – domin_1990 Apr 20 '20 at 10:10

2 Answers2

0

Try using:

<input type="text" class="form-control" id="search" name="search" placeholder="search"
                                       value="{{ request.form['search'].upper() }}"></td>

or

<input type="text" class="form-control" id="search" name="search" placeholder="search"
                                       value="{{ request.form['search'].lower() }}"></td>

basically what you have to do is convert the 'input' and 'whatever you are trying to match' in same case format ie. UPPERCASE or lowercase or Titlecase. I guess you will also have to make same changes in your backend logic where you make actual search.

  • it doesn't work. changed my backend as well. it was like ```if request.method=='POST': search = request.form['search']``` so when i cross verify it with printing the search. it doesnt change – Mayureshk Apr 20 '20 at 10:45
  • yeah.... thats where you need to change actually if request.method=='POST': search = request.form['search'].upper() and if you are using sql to search something in database then use this: "select * from users where upper(first_name) = '"+search+"';" Although the above code is also bad practice from security point of view , you should use string formatting , but i dont remember exactly how that works so i wrote it this way. – Prashant Singh Apr 21 '20 at 14:35
0

You can use the str.upper() method:

{% elif  student.department.upper() != "MATHS DEPARTMENT" %}
    Maths department
{% endif %}

Similarly for lower cases

similarly, student.department.lower() != "maths department"