FLASK - the requested url was not found on the server

17,870

You cannot put the route functions in a class body. That is your problem. Whenever you point your browser to some endpoint (ex: "...com/something"), the Flask app looks for the function decorated with the route @app.route("/something") and calls it.

Therefore, since you place all your routes inside a class body, they cannot be called unless you create an instance of the object (in this case, Flights). So, to fix your problem, simply place all of your route functions outside of the class body.

Share:
17,870
richban
Author by

richban

Updated on June 05, 2022

Comments

  • richban
    richban almost 2 years

    I encounter this error "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again."

    I checked the API's of URL routing It seems to me everything is okay but somehow it doesn't work at all.

    Could you tell me where is the problem?

    My file system looks like this:

    /application.py
    /templates
       /layout.html
       /show_all.html
       /new.html
       /flight.html
    

    End my code:

    from flask import Flask, request, flash, url_for, redirect, render_template, abort
    from flask_sqlalchemy import SQLAlchemy
    
    app = Flask(__name__)
    app.config.from_pyfile('config.cfg')
    db = SQLAlchemy(app)
    
    class Flights(db.Model):
        __tablename__ = 'flights'
        id = db.Column(db.Integer, primary_key=True)
        flight_num = db.Column(db.String)
        airline_name = db.Column(db.String)
        time_time = db.Column(db.Integer)
        date_date = db.Column(db.Integer)
        from_dest = db.Column(db.String)
        to_dest = db.Column(db.String)
        gate_num = db.Column(db.String)
    
        def __init__(self, flight_num, airline_name, time_time, date_date,    from_dest, to_dest, gate_num):
            self.flight_num = flight_num
            self.airline_name = airline_name
            self.time_time = time_time
            self.date_date = date_date
            self.from_dest = from_dest
            self.to_dest = to_dest
            self.gate_num = gate_num
    
        @app.route('/')
        def show_all():
            return render_template('show_all.html', flights=Flights.query.order_by(Flights.id.desc()).all())
    
        @app.route('/flight/<flight_id>', methods=['GET', 'POST'])
        def show_flight(flight_id):
           return render_template('flight.html', flight=Flights.query.filter(Flights.id==flight_id))
    
    
        @app.route('/new', methods=['GET', 'POST'])
        def new():
            if request.method == 'POST':
                if not request.form['flight_number']:
                    flash('Flight number is required', 'error')
                elif not request.form['airline_name']:
                    flash('Airline name is required', 'error')
                elif not request.form['time_time']:
                    flash('Time is required', 'error')
                elif not request.form['date_date']:
                    flash('date is required', 'error')
                elif not request.form['from_dest']:
                    flash('From Destination is required', 'error')
                elif not request.form['to_dest']:
                    flash('To destination is required', 'error')
                elif not request.form['gate_num']:
                    flash('Gate is required', 'error')
                else:
                    flight = Flights(request.form['flight_number'], request.form['airline_name'],request.form['time_time'], request.form['date_date'],request.form['from_dest'], request.form['to_dest'], request.form['gate_num'])
    
                db.session.add(flight)
                db.session.commit()
                flash(u'Flight successfully created')
                return redirect(url_for('show_all'))
            return render_template('new.html')
    
        #@app.route('/delete', methods=['GET', 'POST'])
        #def delete(flight_id):
            #delete_flight = Flights.query.filter(Flights.id=flight_id)
            #db.session.delete(delete_flight)
            #db.session.commit()
    
        @app.route('/update', methods=['POST'])
        def update_done():
            Flights.query.all()
            flash('Updated!')
            db.session.commit()
            return redirect(url_for('show_all'))
    
    if __name__ == '__main__':
        app.run()