I am currently facing an error especially when I run my flask .. The erro description is "AttributeError: 'tuple' object has no attribute 'values'"
The error points at line 11, and 331 which is defined in the function def course_options.
The error is found on this definition"options = [ tuple(course.values()) for course in courses]"
What I have tried:
1
from
flask
import
render_template, url_for, flash, redirect, request
2
from
.
import
student_bp
3
from
app.student.forms
import
StudentForm, CollegeForm, CoursesForm, SearchStudentForm, SearchCourseForm, SearchCollegeForm
4
from
app
import
student, mysql
6
@student_bp
.route(
'
/'
)
7
@student_bp
.route(
'
/view_students'
, methods=[
"
POST"
,
"
GET"
])
8
def
view_students():
9
form = StudentForm()
10
search_form = SearchStudentForm()
11
form.course_code.choices = course_options()
12
cur = mysql.connection.cursor()
13
cur.execute(
'
'
'
SELECT * FROM student '
'
'
)
14
students_data = cur.fetchall()
15
return
render_template(
'
student/view_students.html'
,
16
students_data=students_data,
17
search_form=search_form,
18
form=form)
20
@student_bp
.route(
'
/view_courses'
, methods=[
"
POST"
,
"
GET"
])
21
def
view_courses():
22
form = CoursesForm()
23
search_form = SearchCourseForm()
24
cur = mysql.connection.cursor()
25
cur.execute(
'
'
'
SELECT * FROM course '
'
'
)
26
courses_data = cur.fetchall()
27
form.college.choices = college_options()
28
return
render_template(
'
course/view_courses.html'
,
29
courses_data=courses_data,
30
form=form,
31
search_form=search_form)
33
@student_bp
.route(
'
/view_colleges'
, methods=[
"
POST"
,
"
GET"
])
34
def
view_colleges():
35
form = CollegeForm()
36
search_form = SearchCollegeForm()
37
cur = mysql.connection.cursor()
38
cur.execute(
'
'
'
SELECT * FROM college '
'
'
)
39
colleges_data = cur.fetchall()
40
return
render_template(
'
college/view_colleges.html'
,
41
colleges_data=colleges_data,
42
form=form,
43
search_form=search_form)
46
@student_bp
.route(
'
/add_student'
, methods=[
"
POST"
,
"
GET"
])
48
def
add_student():
49
form = StudentForm()
52
form.course.choices = course_options()
53
if
form.validate_on_submit():
54
id_number = form.id_number.data
55
first_name = form.first_name.data
56
last_name = form.last_name.data
57
course_code = form.course_code.data
58
yr_level = form.year_level.data
59
gender = form.gender.data
60
cur = mysql.connection.cursor()
61
cur.execute(
'
'
'
INSERT INTO student VALUES(%s,%s,%s,%s,%s,%s) '
'
'
,(id_number,first_name, last_name, course_code, yr_level, gender))
62
mysql.connection.commit()
63
flash(
"
Student {} has been added, successfully!"
.
format
(id_number),
"
success"
)
64
return
redirect(url_for(
'
view_students'
))
65
cur.close()
66
return
render_template(
'
student/view_students.html'
, title=
'
Add Student'
, form=form)
68
@student_bp
.route(
'
/add_course'
, methods=[
"
POST"
,
"
GET"
])
69
def
add_course():
70
form = CoursesForm()
71
form.college.choices = college_options()
72
if
form.validate_on_submit():
73
course_code = form.course_code.data
74
course_name = form.course_name.data
75
college_code = form.college_code.data
76
cur = mysql.connection.cursor()
77
cur.execute(
'
'
'
INSERT INTO course VALUES(%s,%s,%s) '
'
'
,(course_code,course_name, college_code))
78
mysql.connection.commit()
79
flash(
"
Course has been added, successfully!"
,
"
success"
)
80
return
redirect(url_for(
'
view_courses'
))
81
cur.close()
82
return
render_template(
'
course/view_courses.html'
, title =
'
Add Course'
, form = form)
84
@student_bp
.route(
'
/add_college'
, methods=[
"
POST"
,
"
GET"
])
85
def
add_college():
86
form = CollegeForm()
87
cur = mysql.connection.cursor()
88
if
form.validate_on_submit():
89
college_code = form.college_code.data
90
college_name = form.college_name.data
91
cur = mysql.connection.cursor()
92
cur.execute(
'
'
'
INSERT INTO college VALUES(%s,%s) '
'
'
,(college_code,college_name))
93
mysql.connection.commit()
94
flash(
"
College has been added, successfully!"
,
"
success"
)
95
return
redirect(url_for(
'
view_colleges'
))
96
cur.close()
97
return
render_template(
'
course/view_colleges.html'
, title =
'
Add Course'
, form = form)
100
@student_bp
.route(
'
/edit_student/<id_number>'
, methods=[
"
POST"
,
"
GET"
])
101
def
edit_student(id_number):
102
cursor = mysql.connection.cursor()
103
cursor.execute(
'
'
'
SELECT * FROM student WHERE school_id = %s '
'
'
, (id_number,))
104
existing_data = cursor.fetchall()
105
data = [
tuple
(data.values())
for
data
in
existing_data]
106
form = StudentForm()
107
form.course_code.choices = course_options()
108
if
form.validate_on_submit():
109
first_name = form.first_name.data
110
last_name = form.last_name.data
111
id_ = form.id_number.data
112
course_code = form.course_code.data
113
yr_level = form.year_level.data
114
gender = form.gender.data
115
cur = mysql.connection.cursor()
116
cur.execute(
'
'
'
UPDATE students SET school_id = %s,
117
first_name = %s,
118
last_name = %s,
119
course_code = %s,
120
year = %s,
121
gender = %s
122
WHERE id_number = %s '
'
'
,(id_, first_name, last_name, course_code, yr_level, gender, id_number))
123
mysql.connection.commit()
124
flash(
"
Student has been updated, successfully!"
,
"
success"
)
125
return
redirect(url_for(
'
view_students'
))
126
cur.close()
128
elif
request.method ==
"
GET"
:
129
form.id_number.data = data[
0
][
0
]
130
form.first_name.data = data[
0
][
1
]
131
form.last_name.data = data[
0
][
2
]
132
form.course_code.data = data[
0
][
3
]
133
form.year_level.data = data[
0
][
4
]
134
form.gender.data = data[
0
][
5
]
135
form.submit.label.text =
"
Update"
136
return
render_template(
'
student/edit_student.html'
,
137
title =
'
Update Student'
,
138
id_number=id_number,
139
form=form)
141
@student_bp
.route(
'
/edit_course/<course_code>'
, methods=[
"
POST"
,
"
GET"
])
142
def
edit_course(course_code):
143
cursor = mysql.connection.cursor()
144
cursor.execute(
'
'
'
SELECT * FROM course WHERE course_code = %s '
'
'
, (course_code,))
145
existing_data = cursor.fetchall()
146
data = [
tuple
(data.values())
for
data
in
existing_data]
147
form = CoursesForm()
148
form.college_code.choices = college_options()
149
if
form.validate_on_submit():
150
course_code1=form.course_code.data
151
course_name = form.course_name.data
152
college_code = form.college_code.data
153
cur = mysql.connection.cursor()
154
cur.execute(
'
'
'
UPDATE course SET name = %s,
155
college_code = %s
156
WHERE course_code = %s '
'
'
,( course_name, college_code, course_code1,))
157
mysql.connection.commit()
158
flash(
"
Course has been updated, successfully!"
,
"
success"
)
159
return
redirect(url_for(
'
view_courses'
))
160
cur.close()
162
elif
request.method ==
"
GET"
:
163
form.course_code.data = data[
0
][
0
]
164
form.course_name.data = data[
0
][
1
]
165
form.college_code.data = data[
0
][
2
]
166
form.submit.label.text =
"
Update"
167
return
render_template(
'
course/edit_courses.html'
,
168
title =
'
Update Student'
,
169
form = form)
171
@student_bp
.route(
'
/edit_college/<college_code>'
, methods=[
"
POST"
,
"
GET"
])
172
def
edit_college(college_code):
173
cursor = mysql.connection.cursor()
174
cursor.execute(
'
'
'
SELECT * FROM college WHERE college_code = %s '
'
'
, (college_code,))
175
existing_data = cursor.fetchall()
176
data = [
tuple
(data.values())
for
data
in
existing_data]
177
form = CollegeForm()
178
if
form.validate_on_submit():
179
college_code1=form.college_code.data
180
college_name = form.college_name.data
181
cur = mysql.connection.cursor()
182
cur.execute(
'
'
'
UPDATE college SET name = %s
183
WHERE college_code = %s '
'
'
,( college_name, college_code1,))
184
mysql.connection.commit()
185
flash(
"
College has been updated, successfully!"
,
"
success"
)
186
return
redirect(url_for(
'
view_colleges'
))
187
cur.close()
189
elif
request.method ==
"
GET"
:
190
form.college_code.data = data[
0
][
0
]
191
form.college_name.data = data[
0
][
1
]
192
form.submit.label.text =
"
Update"
193
return
render_template(
'
college/edit_college.html'
,
194
title =
'
Update College'
,
195
form = form)
198
@student_bp
.route(
'
/del_student/<id_number>'
, methods=[
"
POST"
,
"
GET"
])
199
def
del_student(id_number):
200
form = StudentForm()
201
cursor = mysql.connection.cursor()
202
cursor.execute(
"
DELETE FROM student WHERE school_id = %s"
, (id_number,))
203
cursor.close()
204
mysql.connection.commit()
205
flash(
"
{}'s record has been deleted, successfully!"
.
format
(id_number),
206
"
danger"
)
207
return
redirect((url_for(
"
view_students"
)))
209
@student_bp
.route(
'
/del_course/<course_code>'
, methods=[
"
POST"
,
"
GET"
])
210
def
del_course(course_code):
211
cursor = mysql.connection.cursor()
212
cursor.execute(
"
DELETE FROM course WHERE course_code = %s"
, (course_code,))
213
cursor.close()
214
mysql.connection.commit()
215
flash(
"
{} course has been deleted, successfully!"
.
format
(course_code),
216
"
danger"
)
217
return
redirect((url_for(
"
view_courses"
)))
219
@student_bp
.route(
'
/del_college/<college_code>'
, methods=[
"
POST"
,
"
GET"
])
220
def
del_college(college_code):
221
cursor = mysql.connection.cursor()
222
cursor.execute(
"
DELETE FROM college WHERE college_code = %s"
, (college_code,))
223
cursor.close()
224
mysql.connection.commit()
225
flash(
"
College has been deleted, successfully!"
,
226
"
danger"
)
227
return
redirect((url_for(
"
view_colleges"
)))
230
@student_bp
.route(
'
/student_search'
, methods=[
"
POST"
,
"
GET"
])
231
def
student_search():
232
search_form = SearchStudentForm()
233
form = StudentForm()
234
field = search_form.search_field.data
235
searchby = search_form.search_by.data
236
cursor = mysql.connection.cursor()
237
if
(searchby ==
'
all'
):
238
cursor.execute(
'
'
'
SELECT * FROM student WHERE school_id REGEXP %s or
239
first_name REGEXP %s or
240
last_name REGEXP %s or
241
course_code REGEXP %s or
242
year REGEXP %s or
243
gender REGEXP %s '
'
'
, ([field], [field], [field], [field], [field], [field]))
244
students_data = cursor.fetchall()
245
if
(searchby ==
'
school_id'
):
246
cursor.execute(
'
'
'
SELECT * FROM students WHERE school_id REGEXP %s '
'
'
, [field])
247
students_data = cursor.fetchall()
248
if
(searchby ==
'
first_name'
):
249
cursor.execute(
'
'
'
SELECT * FROM students WHERE first_name REGEXP %s '
'
'
, [field])
250
students_data = cursor.fetchall()
251
if
(searchby ==
'
last_name'
):
252
cursor.execute(
'
'
'
SELECT * FROM students WHERE last_name REGEXP %s '
'
'
, [field])
253
students_data = cursor.fetchall()
254
if
(searchby ==
'
course'
):
255
cursor.execute(
'
'
'
SELECT * FROM students WHERE course REGEXP %s '
'
'
, [field])
256
students_data = cursor.fetchall()
257
if
(searchby ==
'
year'
):
258
cursor.execute(
'
'
'
SELECT * FROM students WHERE year REGEXP %s '
'
'
, [field])
259
students_data = cursor.fetchall()
260
if
(searchby ==
'
gender'
):
261
cursor.execute(
'
'
'
SELECT * FROM students WHERE gender REGEXP %s '
'
'
, [field])
262
students_data = cursor.fetchall()
263
flash(
"
Search results for \" {} \""
.
format
(field),
264
"
success"
)
265
return
render_template(
'
student/view_students.html'
,
266
students_data=students_data,
267
search_form=search_form,
268
form=form,
269
title=
'
Search Results'
)
271
@student_bp
.route(
'
/course_search'
, methods=[
"
POST"
,
"
GET"
])
272
def
course_search():
273
search_form = SearchCourseForm()
274
form = CoursesForm()
275
field = search_form.search_field.data
276
searchby = search_form.search_by.data
277
cursor = mysql.connection.cursor()
278
if
(searchby ==
'
all'
):
279
cursor.execute(
'
'
'
SELECT * FROM course WHERE course_code REGEXP %s or
280
name REGEXP %s or
281
college_code REGEXP %s '
'
'
, ([field], [field], [field]))
282
courses_data = cursor.fetchall()
283
if
(searchby ==
'
course_code'
):
284
cursor.execute(
'
'
'
SELECT * FROM course WHERE course_code REGEXP %s '
'
'
, [field])
285
courses_data = cursor.fetchall()
286
if
(searchby ==
'
name'
):
287
cursor.execute(
'
'
'
SELECT * FROM course WHERE name REGEXP %s '
'
'
, [field])
288
courses_data = cursor.fetchall()
289
if
(searchby ==
'
college_code'
):
290
cursor.execute(
'
'
'
SELECT * FROM course WHERE college_code REGEXP %s '
'
'
, [field])
291
courses_data = cursor.fetchall()
292
flash(
"
Search results for \" {} \""
.
format
(field),
293
"
success"
)
294
return
render_template(
'
course/view_courses.html'
,
295
courses_data=courses_data,
296
search_form=search_form,
297
form=form,
298
title=
'
Search Results'
)
300
@student_bp
.route(
'
/college_search'
, methods=[
"
POST"
,
"
GET"
])
301
def
college_search():
302
search_form = SearchCollegeForm()
303
form = CollegeForm()
304
field = search_form.search_field.data
305
searchby = search_form.search_by.data
306
cursor = mysql.connection.cursor()
307
if
(searchby ==
'
all'
):
308
cursor.execute(
'
'
'
SELECT * FROM college WHERE college_code REGEXP %s or
309
name REGEXP %s '
'
'
, ([field], [field]))
310
colleges_data = cursor.fetchall()
311
if
(searchby ==
'
college_code'
):
312
cursor.execute(
'
'
'
SELECT * FROM college WHERE college_code REGEXP %s '
'
'
, [field])
313
colleges_data = cursor.fetchall()
314
if
(searchby ==
'
name'
):
315
cursor.execute(
'
'
'
SELECT * FROM college WHERE name REGEXP %s '
'
'
, [field])
316
colleges_data = cursor.fetchall()
317
flash(
"
Search results for \" {} \""
.
format
(field),
318
"
success"
)
319
return
render_template(
'
college/view_colleges.html'
,
320
colleges_data=colleges_data,
321
search_form=search_form,
322
form=form,
323
title=
'
Search Results'
)
326
def
course_options():
327
cursor = mysql.connection.cursor()
328
cursor.execute(
'
'
'
SELECT course_code, name, college_code FROM course '
'
'
)
329
courses = cursor.fetchall()
331
options = [
tuple
(course.values())
for
course
in
courses]
334
return
options
336
def
college_options():
337
cursor = mysql.connection.cursor()
338
cursor.execute(
'
'
'
SELECT college_code, name FROM college '
'
'
)
339
colleges = cursor.fetchall()
340
options = [
tuple
(college.values())
for
college
in
colleges]
342
return
options
329
courses = cursor.fetchall()
331
options = [
tuple
(course.values())
for
course
in
courses]
The
fetchall
method returns an array of rows, and each row is a
tuple
of the columns read from the database. So the expression
course.values()
on line 331, is trying to call the
values
method of a
tuple
object named
course
. And as can be seen in
Built-in Types — Python 3.11.0 documentation
[
^
], there is no such method.
But since
course
is already a
tuple
you can just write:
options = [ course
for
course
in
courses]
or even:
options = cursor.fetchall()
And that will give a
list
of
tuple
s, which may, or may not, be what you want.
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
def college_search():
search_form = SearchCollegeForm()
form = CollegeForm()
field = search_form.search_field.data
searchby = search_form.search_by.data
cursor = mysql.connection.cursor()
if(searchby == 'all'):
cursor.execute(''' SELECT * FROM college WHERE college_code REGEXP %s or
name REGEXP %s ''', ([field], [field]))
colleges_data = cursor.fetchall()
if(searchby == 'college_code'):
cursor.execute(''' SELECT * FROM college WHERE college_code REGEXP %s ''', [field])
colleges_data = cursor.fetchall()
if(searchby == 'name'):
cursor.execute(''' SELECT * FROM college WHERE name REGEXP %s ''', [field])
colleges_data = cursor.fetchall()
flash("Search results for \" {} \"".format(field),
"success")
return render_template('college/view_colleges.html',
colleges_data=colleges_data,
search_form=search_form,
form=form,
title='Search Results')
def course_options():
cursor = mysql.connection.cursor()
cursor.execute(''' SELECT course_code, name, college_code FROM course ''')
courses = cursor.fetchall()
options = [ tuple(course.values()) for course in courses]
return options
def college_options():
cursor = mysql.connection.cursor()
cursor.execute(''' SELECT college_code, name FROM college ''')
colleges = cursor.fetchall()
options = [tuple(college.values()) for college in colleges]
return options