【发布时间】:2022-11-28 14:46:20
【问题描述】:
我正在尝试使用数据库中的数据在我的 webapp 中填充课程选择字段。 这是我的尝试。
这是表格 `
class StudentForm(FlaskForm):
idnumber = StringField('ID Number', [validators.DataRequired(), validators.Length(min=9, max=9)])
fname = StringField('First Name', [validators.DataRequired(), validators.Length(max=50)])
mname = StringField('Middle Name', [validators.Length(max=50)])
lname = StringField('Last Name', [validators.DataRequired(), validators.Length(max=50)])
gender = SelectField('Gender', choices=gengen)
yearlvl = SelectField('Year Level', choices= year_level)
course = SelectField('Course', choices= models.Courses.populate())
submit = SubmitField("Save")
`
@classmethod
def populate(cls):
curs = mysql.connection.cursor()
sql = curs.execute("SELECT COURSEID from courses")
if sql > 0:
result = curs.fetchall()
return result
'
当我运行程序时出现此错误
`
File "C:\laragon\SISwebapp\webapp\students\forms.py", line 15, in StudentForm
course = SelectField('Course', choices= models.Courses.populate())
File "C:\laragon\SISwebapp\webapp\models.py", line 87, in populate
curs = mysql.connection.cursor()
AttributeError: 'NoneType' object has no attribute 'cursor'
` 我似乎无法弄清楚出了什么问题..
【问题讨论】:
标签: python database flask web-applications laragon