Home > Mobile >  Getting a 404 not found when running a python and flask app
Getting a 404 not found when running a python and flask app

Time:01-20

Good Evening all , first time putting a job up here i am working on a python and Flask app and no matter what i change i keep on getting a error when i load the first page Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. I am sure it is something i am doing that is small, i am dyslexic so 9 times out of 10 it is just something i am not seeing

This is Search.html

    <!-- Search Bar -->
<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-  
        scale=1">
        <link rel="stylesheet" 
        href="https://cdnjs.cloudflare.com/ajax/libs/font-
        awesome/4.7.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="/static/index.css">
    </head>
    <body>
        <h1><center>Library</center></h1>
        <form  method="post" action="" 
         style="margin:auto;max-width:600px">
            <input type="text" placeholder="Search by author or 
             book, or all to see all the data" name="book">
            <button type="submit"><i ></i>
            </button>
        </form>
        <p></p>
        <center>
        {% for item in data %}
            <tr>
                <td> {{item[0]}} by {{item[1]}}</td>
                </br>
            </tr>
        {% endfor %}
        </center>
    </body>
</html>

and this is libary.py

# library.py
from flask import Flask, render_template, request, redirect
from flaskext.mysql import MySQL
app = Flask(__name__)

# Database connection info. Note that this is not a secure connection.
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = '*******'
app.config['MYSQL_DATABASE_DB'] = 'flightdata'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'

mysql = MySQL()
mysql.init_app(app)
conn = mysql.connect()
cursor = conn.cursor()

#endpoint for search
@app.route('/search', methods=['GET', 'POST'])
def search():
    if request.method == "POST":
        fn = request.form['fn']
        # search by Flight Number
        cursor.execute("SELECT origin, flightNumbers from flightinfo WHERE flightNumbers LIKE %s ", (fn , fn))
        conn.commit()
        data = cursor.fetchall()
        if len(data) == 0 and fn == 'all': 
            cursor.execute("SELECT origin, flightNumbers from flightinfo")
            conn.commit()
            data = cursor.fetchall()
        return render_template('search.html', data=data)
    return render_template('search.html')


if __name__ == '__main__':
    app.debug = True
    app.run()

Thank you for you help in advance

CodePudding user response:

Based on the comment, the URL you're entering is wrong, since you've added the route:

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

You should enter the URL 127.0.0.1:5000/search

  •  Tags:  
  • Related