Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask and Python how to make search engine for data from mysql database

I want to make some kind of search engine for student's information by entering their first name in html input field, but I have some troubles with my code. I am using Flask with Python though. Here is my project.py code:

@app.route('/search', methods=['GET', 'POST'])
def search():
    if request.method == "POST":
        db = MySQLdb.connect(user="root", passwd="", db="cs324", host="127.0.0.1")
        c=db.cursor()
        c.executemany('''select * from student where name = %s''', request.form['search'])
        for r in c.fetchall():
            print r[0],r[1],r[2]
            return redirect(url_for('search'))
    return render_template('search.html')

Here is my search.html code:

{% extends "hello.html" %}
{% block content %}
<div class="search">
<form action="" method=post>
    <input type=text name=search value="{{ request.form.search}}"></br>
    <div class="actions"><input type=submit value="Search"></div>
</form>
</div>
{% for message in get_flashed_messages() %}
<div class=flash>
    {{ message }}
</div>
{% endfor %}
{% endblock %}

When I hit Search button nothing happens, I checked database it has some data in it so it is not empty, I can't find where am I making a mistake, please help?

like image 991
Srdan Ristic Avatar asked May 07 '14 15:05

Srdan Ristic


People also ask

Can Flask be used with MySQL?

Flask-MySQL is a Flask extension that allows you to access a MySQL database. You can report bugs and discuss features on the issues page.

How do you query a database in Flask?

You then create a Flask application instance called app , which you use to configure two Flask-SQLAlchemy configuration keys: SQLALCHEMY_DATABASE_URI : The database URI to specify the database you want to establish a connection with. In this case, the URI follows the format sqlite:/// path/to/database. db .


1 Answers

Make sure, action point to proper url

I think you render the form with wrong action for submitting the form.

Your version is using action="" and I guess, it shall be action="/search"

So your template shall be changed like:

{% extends "hello.html" %}
{% block content %}
<div class="search">
<form action="/search" method=post>
    <input type=text name=search value="{{ request.form.search}}"></br>
    <div class="actions"><input type=submit value="Search"></div>
</form>
</div>
{% for message in get_flashed_messages() %}
<div class=flash>
    {{ message }}
</div>
{% endfor %}
{% endblock %}

Do not redirect out of your result

Your existing code is processing POST, but within first loop it ends up returning with redirect

@app.route('/search', methods=['GET', 'POST'])
def search():
    if request.method == "POST":
        db = MySQLdb.connect(user="root", passwd="", db="cs324", host="127.0.0.1")
        c=db.cursor()
        c.executemany('''select * from student where name = %s''', request.form['search'])
        for r in c.fetchall():
            print r[0],r[1],r[2]
            return redirect(url_for('search')) # <- Here you jump away from whatever result you create
    return render_template('search.html')

Do render your template for final report

Your code does not show in POST branch any attempt to render what you have found in the database.

Instead of print r[0], r[1]... you shall call render_template()

Something like this

@app.route('/search', methods=['GET', 'POST'])
def search():
    if request.method == "POST":
        db = MySQLdb.connect(user="root", passwd="", db="cs324", host="127.0.0.1")
        c=db.cursor()
        c.executemany('''select * from student where name = %s''', request.form['search'])
        return render_template("results.html", records=c.fetchall())
    return render_template('search.html')
like image 111
Jan Vlcinsky Avatar answered Sep 20 '22 00:09

Jan Vlcinsky