Flask SQLaclemy AttributeError:'NoneType'对象没有属性'all'

时间:2018-04-10 01:04:35

标签: python flask flask-sqlalchemy

我正在尝试接受请求并使用该变量,然后在SELECT * FROM(VARIABLE)中为我的数据库使用该变量,

它在python命令行中工作,但在我的文件中不能渲染模板。

这是我的代码,对不起,我是新手。

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:@localhost/bustedin2'
SQLALCHEMY_TRACK_MODIFICATIONS = False
db = SQLAlchemy(app)


class Offender2(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    Name = db.Column(db.String(80), unique=False, nullable=False)
    DOB = db.Column(db.String(120), unique=False, nullable=False)
    Sex = db.Column(db.String(80), unique=False, nullable=False)
    Race = db.Column(db.String(120), unique=False, nullable=False)
    Charge = db.Column(db.String(80), unique=False, nullable=False)
    ChargeLevel = db.Column(db.String(120), unique=False, nullable=False)
    ArrestDate = db.Column(db.String(80), unique=False, nullable=False)
    City = db.Column(db.String(120), unique=False, nullable=False)
    State = db.Column(db.String(80), unique=False, nullable=False)
    BookingNumber = db.Column(db.String(120), unique=False, nullable=False)
    ArrestDate = db.Column(db.String(80), unique=False, nullable=False)
    AddedToDataBaseDate = db.Column(db.String(120), unique=False, nullable=False)


# notice that all models inherit from SQLAlchemy's db.Model

# Make the WSGI interface available at the top level so wfastcgi can get it.
wsgi_app = app.wsgi_app


@app.route('/')
def show_all():
    return render_template('ShowAll.html', offenders=Offender2.query.all())


@app.route("/Details/<int:id>")
def show_one(id):
    found_Offender = Offender2.query.get(id)
    return render_template('details.html', offender=found_Offender)


# @app.route('/Details/Multi/<int:BookingNumber>')
# def get_booking():
# BookingNumber = request.args.get('BookingNumber')
# Found_Booking = Offender2.query.filter(Offender2.BookingNumber == "BookingNumber")
# return render_template('ShowMultible.html', offenders = Found_Booking)

@app.route('/Details/Multi/<int:BookingNumber>')
def Show_Multi(BookingNumber):
    Found_Booking = Offender2.query.get("BookingNumber").all()
    return render_template('ShowMultible.html', offenders=Found_Booking)

这是我有问题的地方..

 @app.route('/Details/Multi/<int:BookingNumber>')
    def Show_Multi(BookingNumber):
        Found_Booking = Offender2.query.get("BookingNumber").all()
        return render_template('ShowMultible.html', offenders=Found_Booking)

1 个答案:

答案 0 :(得分:0)

@app.route('/Details/Multi/<int:BookingNumber>')
def Show_Multi(BookingNumber):
    Found_Booking = Offender2.query.get("BookingNumber").all()
    return render_template('ShowMultible.html', offenders=Found_Booking)

在此段代码中,您正在对主键等于字符串“BookingNumber”(而不是变量)的Offender2条目执行查询。此外,如果您使用get,则会返回该对象,因此您无需致电all()。尝试将代码更改为:

Found_Booking = Offender2.query.get(BookingNumber)

修改

刚刚意识到BookingNumber不是你的主键,所以这不起作用。您需要通过过滤BookingNumber字段来获得结果。

Found_Booking = Offender2.query.filter_by(BookingNumber=BookingNumber).all()

此外,数据库中的BookingNumber存储为字符串,因此您应该从int更改路径定义:

@app.route('/Details/Multi/<BookingNumber>')