Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am currently experiencing a problem in the function that I have created for my route
The error description is - TypeError: edit() missing 1 required positional argument: 'id_number'

@student_bp.route('/student/edit', methods=['POST','GET'])
def edit(id_number):
cursor = mysql.connection.cursor()
cursor.execute(''' SELECT * FROM student WHERE school_id = %s ''', (id_number,))
existing_data = cursor.fetchall()
data = [tuple(data.values()) for data in existing_data]
form = StudentForm()
if form.validate_on_submit():
first_name = form.first_name.data
last_name = form.last_name.data
id_ = form.id.data
course_code = form.course_code.data
yr_level = form.year.data
gender = form.gender.data
cur = mysql.connection.cursor()
cur.execute(''' UPDATE students SET school_id = %s,
first_name = %s,
last_name = %s,
course_code = %s,
year = %s,
gender = %s
WHERE id_number = %s ''',(id_, first_name, last_name, course_code, yr_level, gender, id_number))
mysql.connection.commit()
flash("Student has been updated, successfully!", "success")
return redirect('/student')

elif request.method == "GET":
form.id.data = data[0][0]
form.first_name.data = data[0][1]
form.last_name.data = data[0][2]
form.course_code.data = data[0][3]
form.year.data = data[0][4]
form.gender.data = data[0][5]
form.submit.label.text = "Update"
return render_template('edit.html',
title = 'Update Student',
id_number=id_number,
form=form)

What I have tried:

@student_bp.route('/student/edit', methods=['POST','GET'])
def edit(id_number):
	cursor = mysql.connection.cursor()
	cursor.execute(''' SELECT * FROM student WHERE school_id = %s ''', (id_number,))
	existing_data = cursor.fetchall()
	data = [tuple(data.values()) for data in existing_data]
	form = StudentForm()
	if form.validate_on_submit():
		first_name = form.first_name.data
		last_name = form.last_name.data
		id_ = form.id.data
		course_code = form.course_code.data
		yr_level = form.year.data
		gender = form.gender.data
		cur = mysql.connection.cursor()
		cur.execute(''' UPDATE students SET school_id = %s,
											first_name = %s, 
											last_name = %s, 
											course_code = %s, 
											year = %s, 
											gender = %s 
										WHERE id_number = %s ''',(id_, first_name, last_name, course_code, yr_level, gender, id_number))
		mysql.connection.commit()
		flash("Student has been updated, successfully!", "success")
		return redirect('/student')
		
	elif request.method == "GET":
		form.id.data = data[0][0]
		form.first_name.data = data[0][1]
		form.last_name.data = data[0][2]
		form.course_code.data = data[0][3]
		form.year.data = data[0][4]
		form.gender.data = data[0][5]
		form.submit.label.text = "Update"
	return render_template('edit.html', 
							title = 'Update Student', 
							id_number=id_number,
							form=form)
Posted
Updated 8-Nov-22 5:17am

It's not in that code: it's in the code that calls that code.

The edit method you defined in your question requires a parameter which is stored in a local variable called id_number which is only accessible inside the edit method itself. When you call the method, you must supply an argument to be loaded into that variable:
Python
edit(666)
Or
Python
edit("Hello world")
perhaps.
If you call it like this:
Python
edit()
Then you don't provide that argument, and the system rightly complains.
 
Share this answer
 
You need something like the following in your edit template:
HTML
  <form action="{{ url_for('edit', id_number=post['id_number']) }}" method="post">
//                                  ^ item to be sent to the edit method

But that is guesswork as you have not shown your template.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900