Вопрос

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?

Это было полезно?

Решение

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')
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top