“DefaultMeta”对象不可编辑

2024-06-16 13:48:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习cs50教程04,从中我得到了以下错误

错误:-“DefaultMeta”对象不可编辑, 请检查我的代码我认为问题是从如果开始的声明,但我无法解决问题,请帮助

from flask import Flask, render_template, request
from models import *

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://postgres:password@localhost:5432/postgres"
app.config["SQLALCHEMY_TRACK_MODIFIER_MODIFICATIONS"] = False

db.init_app(app)

@app.route("/")
def indexapp():
    railway = Railway.query.all()
    return render_template("indexapp.html", Railway=Railway)

@app.route("/book", methods=["POST"])
def book():
    """ BOOK A Railway """
    #Get Form Information

    name = request.form.get("name")
    try:
        railway_id = int(request.form.get("railway_id"))
    except ValueError:
        return render_template("error.html", message="Invalid Railway Number.")
    #Make Sure the Railway Exists
    railway = Railway.query.get(flight_id)
    if railway is None:
        return render_template("error.html", message="No Such Railway with this id.")

    #ADD Passenger
    
    passenger = Passenger(name=name, railway_id=railway_id)
    db.session.add(passenger)
    db.session.commit()
    return render_template("sucess.html")

Tags: namefromimportidappdbgetreturn