在呈现html模板时,未正确传递minutes_id。引发错误:“ AttributeError:'InstrumentedList'对象没有属性'get'”。我不确定该如何解决。
这是使我震惊的代码行。
minutes = club.minutes.get(minutes_id) #GET THE Minutes for specific DAY
routes.py:
@clubs.route("/view_minutes/<int:user_id>/<int:club_id>", methods=['GET', 'POST'])
@login_required
def view_minutes(user_id, club_id):
club = Club.query.get_or_404(club_id)
user = User.query.get_or_404(user_id)
minutes = club.minutes
#if click "export" -->
#return render_pdf(url_for('minutes_pdf', user_id=user_id, club_id=club_id))
return render_template('view_minutes.html', title='View', user=user, club=club, minutes=minutes)
@clubs.route("/minutes_pdf/<int:user_id>/<int:club_id>/<int:minutes_id>.pdf", methods=['GET', 'POST'])
@login_required
def minutes_pdf(user_id, club_id, minutes_id):
club = Club.query.get_or_404(club_id)
user = User.query.get_or_404(user_id)
minutes = club.minutes.get(minutes_id) #GET THE Minutes for specific DAY
return render_template('view_minutes_pdf.html', club=club, minutes=minutes)
models.py
user_club_assoc_table = db.Table('user_club_assoc_table',
db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
db.Column('club_id', db.Integer, db.ForeignKey('club.id')))
roles = db.relationship('Role', secondary='user_roles',
backref=db.backref('users', lazy='dynamic'))
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
firstname = db.Column(db.String(15), nullable=False)
lastname = db.Column(db.String(15), nullable=False)
email = db.Column(db.String(60), unique=True, nullable=False)
password = db.Column(db.String(60), nullable=False)
role = db.Column(db.Integer(), nullable=False, default=ROLES['student'])
clubs = db.relationship('Club', secondary=user_club_assoc_table)
def __repr__(self):
return f'{self.firstname} {self.lastname}' #return f'User(firstname={self.firstname!r}, lastname={self.lastname!r})'
class Club(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True, nullable=False)
members = db.relationship('User', secondary=user_club_assoc_table)
minutes = db.relationship('Minutes', backref='club')
def __repr__(self):
return f'{self.name}'#Club(name={self.name!r})
class Minutes(db.Model):
id = db.Column(db.Integer, primary_key=True)
club_id = db.Column(db.Integer, db.ForeignKey('club.id'))
date = db.Column(db.Date, nullable=False) #0000-00-00
time = db.Column(db.Time) #00:00:00
location = db.Column(db.String(100), nullable=False)
attendance = db.relationship('Attendance', backref='minutes', lazy=True) #check code
purchase = db.Column(db.Text)
purchasemotion = db.Column(db.Text)
fundraiser = db.Column(db.Text)
fundmotion = db.Column(db.Text)
minute = db.Column(db.Text, nullable=False) #notes
def __repr__(self):
return f'{self.club_id} {self.date}'
class Attendance(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
student_name = db.Column(db.String(35), nullable=False)
present = db.Column(db.Boolean, default=False) #set correctly
minutes_id = db.Column(db.Integer, db.ForeignKey('minutes.id'))
答案 0 :(得分:0)
您已经在网址中捕获minute_id
,为什么不直接查询
Minutes.query.get_or_404(minute_id)