FLASK - 在服务器上找不到请求的URL

时间:2015-04-10 13:11:14

标签: python flask sqlalchemy flask-sqlalchemy

我遇到此错误“在服务器上找不到请求的网址。如果您手动输入了网址,请检查拼写然后重试。”

我检查了API's of URL routing在我看来一切都还可以,但不知何故它根本不起作用。

你能告诉我问题出在哪里吗?

我的文件系统如下所示:

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

结束我的代码:

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()

1 个答案:

答案 0 :(得分:1)

您不能将路线功能放在类体中。那是你的问题。每当您将浏览器指向某个端点时(例如:&#34; ... com / something&#34;),Flask应用程序会查找用路径@app.route("/something")修饰的功能并调用它。

因此,由于您将所有路径放在类主体中,因此除非您创建对象的实例(在本例中为Flights),否则无法调用它们。因此,要解决您的问题,只需将所有路线功能放在类体外。