How to Fix Typeerror: Insert Expected 2 Arguments, Got 1 in Python Flask?
Typeerror: Insert Expected 2 Arguments, Got 1 So, When I Got Objects in Fuction Get, and Then I Want That Value in My dataUser, Ive Old List (Output) [{...
TypeError: insert expected 2 arguments, got 1
so, when i got objects in fuction get, and then i want that value in my dataUser,
ive old list (output) [{ 'username':'budi'},{ 'username':'jhon'}] and i wanna old list to new list (dataUser)
dataUser = []
print(dataUser)
class users(Resource):
# Get data
def get(self):
query = User.query.all()
output = [
{
"username":data.username
}
for data in query
]
response = {
"code" : 200,
"msg" : "Query data sukses",
"data" : output
}
for item in output:
print(item)
dataUser.insert(item)
return response['msg']
so i can set up those value in Post
def post(self):
UsersUsername = request.form['username']
UsersPassword = request.form['password']
UsersConfPassword = request.form['confPassword']
try:
if(UsersPassword == UsersConfPassword | datauser.username != UsersUsername) : // <-- to this
model = User(username=UsersUsername, password=UsersPassword)
model.save()
response = {
"msg" : "Data berhasil dimasukan",
"code": 200
}
return response['msg']
except:
response = {
"msg" : "Data Tidak Berhasil dimasukan dikarenakan username / password salah",
"code": 400
}
return response["msg"]
3 Answers
insert indeed takes two arguments - the index to insert to and the object to insert. If you just meant to add all the items to the dataUser list, you could use append instead:
dataUser.append(item)
If you want to add a new value to a list, use append to add a new item to the end of the list
dataUser = []
dataUser.append("first item")
insert adds the value at the given positions,
...
dataUsers.insert(1, "second item")
# roughly equivalent to
dataUsers[1] = "second item"
The issue here that you use insert(index, value) method that need two arguments follow doc:
ex from w3school
fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, "orange")
please fix your code with this
dataUser.append(item)
it will work.