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

19,012

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')
Share:
19,012
Srdan Ristic
Author by

Srdan Ristic

I am a dedicated, focused, trustworthy, energetic, organized, hardworking, and dependable individual who excels at learning new technologies and techniques, working individually or in a team setting, capable of completing multiple tasks simultaneously with high-quality results. I mostly enjoy programming in Java, one of the reasons why I pursued and successfully completed a certification as Oracle Certified Associate Java SE 8 developer. I love web programming, especially being involved in both front and back-end within the scope of a full software development cycle. During my internship with Benetech Corporation, I learned various technologies such as Spring Framework, Restful Web Services, AngularJS Framework, Git, etc. This experience was a game changer in my professional life. It not only helped me improve as a developer but it also helped me throughout my undergraduate and graduate studies, where I worked on a variety of projects, involving Java and Web programming as well as data mining and data visualization.

Updated on June 07, 2022

Comments

  • Srdan Ristic
    Srdan Ristic almost 2 years

    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?