gpt4 book ai didi

python - Cherrypy 与 Mysql 错误 : "tuple indices must be integers, not str" when trying to add users

转载 作者:行者123 更新时间:2023-11-29 11:33:33 25 4
gpt4 key购买 nike

伙计们,当我尝试注册用户时,我的项目遇到了这个随机错误。

这是从浏览器返回的信息。

{Gender: "M", ReEmail: "heyguys@whatup.com", Birthday: "1/19/1980", Role: "Athlete",…}
Birthday: "1/19/1980"
Country: "United States"
Email: "heyguys@whatup.com"
Gender: "M"
Password:"doesntwork123"
ReEmail:"heyguys@whatup.com"
RePassword:"doesntwork123"
Role:"Athlete"
kwargs:{}
method:"POST"
results:{error: "tuple indices must be integers, not str"}
error: "tuple indices must be integers, not str"

通过使用 PyCharm 调试器,我将问题集中到这个特定部分

def addUser(self, session, *args, **kwargs):
try:
#existingPerson = session.query(person_models.Player).filter(person_models.Player.Email==args['Email']).first()
print person_models.Player
print person_models.Player.Email
existingPerson = session.query(person_models.Player).filter(person_models.Player.Email == args['Email']).first()
print existingPerson
if existingPerson is not None:
return {'error':"User already exists"}
person_model = getattr(person_models)
person = person_model(*args)
session.add(person)
session.commit()
return {"response": "User added"}
except Exception as e:
return {'error':str(e)}

一旦到达现有人员,它就会直接将我发送到异常。这是当它给我“元组索引必须是整数,而不是str”时

<小时/>

这是一些附加代码。

这是一些代码:

webapi.py

import cherrypy
from api.restBase import jsonDbRest
from person.controllers import PersonController
class PersonAPI:
exposed = True

@jsonDbRest
def GET(self, email=False, **kwargs):
session = cherrypy.request.db
pc = PersonController()
if email in kwargs:
profile = pc.get(session, **kwargs)
return {"results":[profile], "count":'1'}
profile = pc.search(session, **kwargs)
return {"results":profile.to_dict(), "count":'1'}

@jsonDbRest
def POST(self, *args, **kwargs):
session = cherrypy.request.db
pc = PersonController()
response = pc.addUser(session, *args, **kwargs)
return {"results": response}

它正在提示第 15 行。

这是使用代码的另一行:

restbase.py

import cherrypy
import json
from engine import create_session

def jsonDbRest(func):
def wrapper(self, *args, **kwargs):

cherrypy.request.db = create_session()

if 'Content-Length' in cherrypy.request.headers:
length = cherrypy.request.headers['Content-Length']
bodytext = cherrypy.request.body.fp.read(int(length))
else:
bodytext = ""
if bodytext != "":
jsonData = json.loads(bodytext)
else:
jsonData = None
cherrypy.request.json = jsonData
func_data = func(self, *args, **kwargs)
data = {"method":func.__name__, "input":jsonData,"kwargs":kwargs}
if func_data is not None:
data.update(func_data)
cherrypy.response.headers['Content-Type'] = 'application/json'
if "error" in data:
cherrypy.response.status = "400"
else:
cherrypy.response.status = "200"
cherrypy.request.db.close()
return json.dumps(data, indent=4)

返回包装

另一个代码

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.declarative import AbstractConcreteBase
import datetime
import decimal
import netaddr
import sqlalchemy
from time import strftime

class Base(object):
def to_dict(self):
dict = {}
dict = self.__dict__
remove = '_sa_instance_state'
dict = {key: value for key, value in dict.items() if key is not remove}
dict['DateOfBirth'] = dict['DateOfBirth'].strftime('%m/%d/%Yi')

return dict

def from_dict(self, dict):
for col in dict:
setattr(self, col, dict[col])

def to_array(self, columns=[]):
if len(columns) < 1:
columns = self.__mapper__.columns.keys()
data = []
for col in columns:
data.append(getattr(self, col))
return data

def get_columns(self):
return self.__mapper__.columns.keys()

JsonBase = declarative_base(cls=Base)

最佳答案

我认为您的问题出在第一个示例上:

existingPerson = session.query(person_models.Player).filter(person_models.Player.Email == args['Email']).first()

尝试使用 kwargs 而不是 args:

existingPerson = session.query(person_models.Player).filter(person_models.Player.Email == kwargs['Email']).first()

关于python - Cherrypy 与 Mysql 错误 : "tuple indices must be integers, not str" when trying to add users,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36996071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com