关键字Arg后Web2Py语法错误非关键字Arg

时间:2013-05-12 02:38:44

标签: python web2py

我正在尝试在我的localhost上实现此代码:

def form_a():
   form = SQLFORM(db.registration, deletable=True, request.args(0), upload=URL(r=request, f='download'))
   if form.accepts(request.vars, session):
       if not form.record:
           response.flash = "Your input data has been submitted."
       else:
           if form.vars.delete_this_record:
               session.flash = "User record successfully deleted."
           else:
               session.flash = "User record successfully updated."
           redirect(URL(r=request, f='form_a’))
   records = db().select(db.registration.ALL)
   return dict(form=form, records=records)

但是我在此行收到 非关键字arg后关键字arg 错误:

   form = SQLFORM(db.registration, deletable=True, request.args(0), upload=URL(r=request, f='download'))

在此行扫描文字错误 时出现 EOL:

redirect(URL(r=request, f='form_a’))

我正在使用Python 3和Web2Py 2.4.6,谢谢。

3 个答案:

答案 0 :(得分:2)

您有一个非关键字参数:

form = SQLFORM(db.registration, deletable=True, request.args(0), upload=URL(r=request, f='download'))
                                                ^^^^^^^^^^^^^^^

关键字参数后:

form = SQLFORM(db.registration, deletable=True, request.args(0), upload=URL(r=request, f='download'))
                                ^^^^^^^^^^^^^^

您必须使deletable成为非关键字参数或使request.args(0)成为关键字参数。

至于第二个错误,这里的引用实际上并不是一个结束语:

redirect(URL(r=request, f='form_a’))
                                 ^

注意它是如何卷曲的。将其替换为常规单引号。

答案 1 :(得分:1)

所有位置参数必须在关键字参数之前,所以这里request.args(0)导致错误为deletable=True之前传递了关键字参数。

  

form = SQLFORM(db.registration,deletable = True,request.args(0),   upload = URL(r = request,f ='download'))

来自docs

  

在函数调用中,关键字参数必须遵循位置   参数。传递的所有关键字参数必须与其中一个匹配   函数接受的参数及其顺序并不重要。

redirect(URL(r=request, f='form_a’))中,您使用了不同类型的开场和结尾报价。

必须是f='form_a'f="form_a"

答案 2 :(得分:0)

在:

form = SQLFORM(db.registration, deletable=True, request.args(0), upload=URL(r=request, f='download')),

您有deletable=True, request.args(0)这是关键字参数后面的非关键字参数。这是无效的语法..

并在redirect(URL(r=request, f='form_a’))

redirect(URL(r=request, f='form_a’))
                                 ^ This is not what you want..
redirect(URL(r=request, f='form_a'))
                                 ^ This IS what you want..