TypeError:Python Decorator出现意外的关键字参数

TypeError:Python Decorator出现意外的关键字参数,python,flask,decorator,Python,Flask,Decorator,我收到以下类型错误: TypeError: wrapper() got an unexpected keyword argument 'id' 当我尝试使用这些装饰器执行函数时: def owner_required(table): def tags_decorator(func): @wraps(func) # this requires an import def wrapper(id): user_profile = (se

我收到以下类型错误:

TypeError: wrapper() got an unexpected keyword argument 'id'
当我尝试使用这些装饰器执行函数时:

def owner_required(table):
    def tags_decorator(func):
        @wraps(func) # this requires an import
        def wrapper(id):
            user_profile = (session['username'], session['picture'])
            # Connect to the database
            con = connect()
            Base.metadata.bind = con
            # Creates a session
            DBSession = sessionmaker(bind=con)
            dbsession = DBSession()
            if table == 'incidents':
                query = dbsession.query(Incidents).
                    filter_by(case_num=id).first()
            if table == 'audits':
                query = dbsession.query(Audits).filter_by(id=id).first()
            if table == 'actions':
                query = dbsession.query(Actions).filter_by(id=id).first()

            creator = int(query.user_id)
            ses_user = int(session['user_id'])
            if 'username' not in session or creator != ses_user:
                flash("Sorry, %s,"
                      " you are not authorized to edit this incident." %
                      session['username'])
                return redirect('/incidents/')
            else:
                func()
        return wrapper
    return tags_decorator

def check_if_report_exists(table):
    def tags_decorator(func):
        @wraps(func) # this requires an import
        def wrapper(**kwargs):
            # Connect to the database
            con = connect()
            Base.metadata.bind = con
            # Creates a session
            DBSession = sessionmaker(bind=con)
            dbsession = DBSession()
            if table == 'incidents':
                query = dbsession.query(Incidents).filter_by(case_num=id).first()
            if table == 'audits':
                query = dbsession.query(Audits).filter_by(id=id).first()
            if table == 'actions':
                query = dbsession.query(Actions).filter_by(id=id).first()
            if query is None:
                flash("Sorry, %s,"
                      " this report does not exists" %
                      session['username'])
                return redirect('/dashboard/')
            else:
                 func(**kwargs)
        return wrapper
    return tags_decorator
以下是装饰器的功能:

app.route('/incidents/edit/<int:id>/', methods=['GET', 'POST'])
@login_required
@owner_required('incidents')
@check_if_report_exists('incidents')
def editIncident(id):
    some code...
app.route('/incents/edit/',methods=['GET','POST'])
@需要登录
@需要业主单位(“事故”)
@检查报告是否存在(“事件”)
def事件(id):
一些代码。。。
本质上,路由是使用Flask将整数传递给函数,以调用包含正确信息的页面。我需要在decorator中使用相同的号码,以确保登录的用户是创建页面供他们编辑的用户


我一直在关注decorators,特别是关于向decorators传递参数的部分。

这是一个愚蠢的错误-我太关注错误的decorator了。@login\u required未传递任何参数

我通过将(*args,**kwargs)传递给包装器和函数解决了这个问题:

def login_required(session):
    def tags_decorator(func):
        @wraps(func) # this requires an import
        def wrapper(*args, **kwargs):
            logger.info('Checking if user in logged in.')
            if 'username' not in session:
                logger.info('User is not logged in.')
                return redirect('login')
            else:
                logger.info('User is logged in.')
                return func(*args, **kwargs)
        return wrapper
    return tags_decorator

我犯的另一个错误是没有返回func(),因此我收到一个视图错误。我假设这是因为我使用的是Python 3。

这是一个愚蠢的错误-我太专注于错误的装饰程序了。@login\u required未传递任何参数

我通过将(*args,**kwargs)传递给包装器和函数解决了这个问题:

def login_required(session):
    def tags_decorator(func):
        @wraps(func) # this requires an import
        def wrapper(*args, **kwargs):
            logger.info('Checking if user in logged in.')
            if 'username' not in session:
                logger.info('User is not logged in.')
                return redirect('login')
            else:
                logger.info('User is logged in.')
                return func(*args, **kwargs)
        return wrapper
    return tags_decorator

我犯的另一个错误是没有返回func(),因此我收到一个视图错误。我假设这是因为我使用的是Python 3。

对于装饰器,将其包装(*args,**kwargs)作为符号。并将参数传递给func(*args,**kwargs),在代码中,您只需编写func(),我认为您无法按现在的方式堆叠装饰器。最后一个是
check\u if\u report\u exists
,它返回一个函数,其中
id
给出了一个
TypeError
。我认为如果没有所有的代码就无法判断。我尝试添加*args和**kwargs,但收到了相同的错误。我编辑了原始帖子以包含另一个decorator的代码,并更好地解释了我试图实现的目标。并将参数传递给func(*args,**kwargs),在代码中,您只需编写func(),我认为您无法按现在的方式堆叠装饰器。最后一个是
check\u if\u report\u exists
,它返回一个函数,其中
id
给出了一个
TypeError
。我认为如果没有所有的代码就无法判断。我尝试添加*args和**kwargs,但收到了相同的错误。我编辑了原始帖子以包含另一个装饰者的代码,并更好地解释了我试图实现的目标。以下是git repo: