尝试预订航班时出现运行时错误

时间:2020-05-28 11:00:09

标签: python flask jinja2

这是我的代码:

@app.route("/")
def main():
    flights = Flight.query.all()
    return render_template("index.html", flights = flights)


@app.route("/book", methods = ["POST"])
def book():
    name = request.form.get("username")
try:
    flight_id = int(request.form.get("name"))
except ValueError:
    return "Please enter valid flight id."

# Checking whether the flight with the given flight id exists or not
query = Flight.query.get(flight_id)
if query is None:
    return "No such flight found"
else:
    p = Passengers(passenger_name = name, passenger_flight_id = flight_id)
    db.session.add(p)
    db.session.commit()
    return render_template("success.html", success = "Ticket booked succesfully")


if __name__ == "__main__":
    with app.app_context():
        main()
        app.run(port=5000)

我在这里遇到此错误

RuntimeError:应用程序无法创建URL适配器以用于独立于请求的URL生成。您可以通过设置SERVER_NAME配置变量来解决此问题。

这是我的index.html文件

{% extends "layout.html" %}
{% block title %}Book a flight{% endblock %}
{% block body %}
<h1>Book a flight</h1>
<form method="POST" action="{{url_for('book')}}">
    <select name="name">
        {% for flights in flight %}
        <option id="{{flights.id}}">{{flights.origin}} to {{flights.destination}}</option>
        {% endfor %}
    </select>
    <input type="text" name="username" placeholder="Enter Name" />
    <button>Book</button>
</form>
{% endblock %}

0 个答案:

没有答案