Solution 1 :

Have you checked whether the data is getting parsed into your query and is it fetching results from the table? You can check that by using the following code:


after cursor.execute()

and for error number 1:
I will say please verify the name of the HTML file.

p.s there is no need to use conn.commit() as it is advisable to use only when you are creating a record, updating a record or deleting a record from the table, so you can go ahead and delete that part.

Let me know if this helps you or not.

Problem :

I have tried an assignment taken from internet for displaying data from flask to hmtl form.
But the issue here is I’am getting two errors:-
1) The HTML file rendered in flask is not found.
2) The SQL table and columns are not resolved.

I have tried resolving this issues with many solution through internet.
I tried it on Pycharm and using MariaDB Connector, the test connection was successful.
Can Someone solve it for me?

The flask code is as follows :-

from flask import Flask, render_template, request
from flaskext.mysql import MySQL

app = Flask(__name__)
# Database connection info
app.config['MYSQL_DATABASE_USER'] = 'root'

app.config['MYSQL_DATABASE_PASSWORD'] = ''

app.config['MYSQL_DATABASE_DB'] = 'LibraryDB'

app.config['MYSQL_DATABASE_HOST'] = 'localhost'

mysql = MySQL()


conn = mysql.connect()

cursor = conn.cursor()

@app.route('/search', methods=['GET', 'POST'])

def search():

    if request.method == "POST":

        book = request.form['book']

        # search by author or book

        cursor.execute("SELECT name, author from book WHERE name LIKE %s OR author LIKE %s", (book,book))


        data = cursor.fetchall()

        # all in the search box will return all the tuples

        if len(data) == 0 and book == 'all':

            cursor.execute("SELECT name, author from Book")


            data = cursor.fetchall()

        return render_template('search.html', data=data)

    return render_template('search.html')

if __name__ == '__main__':

    app.debug = True

My HTML code is as follows :-

<!DOCTYPE html>
<html lang="utf-8">
        <link rel="stylesheet" href="../static/index.css" type="text/css">
        <h1 style="text-align: center;">Library</h1>
        <form class="example" method="post" action=""
                <input type="text" placeholder="Search by author or book, or all to see all the data" name="book">
            <button type="submit">Search<i class="fa fa-search"></i>
        <div style="text-align: center;">
        {% for item in data %}
                <td> {{item[0]}} by {{item[1]}}</td>
        {% endfor %}

Am fine with CSS so let’s not bother that


Comment posted by Madhav Parikh

You need to specify the exact error like a line the error occurs or share a screenshot