Flask 函数接受一个参数,给定 2 个
Flask function takes one argument, 2 given
我正在尝试使用 form.consumer_key.data
从我的 WTForm 传递数据,但我收到一个参数,说我正在传递两个参数。我做错了什么?
这是我的错误
File "/Users/Gaby/Documents/Code/Twitty/app/views.py", line 18, in authenticate
这是我尝试传递文件的路径
auth = TwitterAuth()
@app.route('/')
@app.route('/authenticate', methods=['GET', 'POST'])
def authenticate():
form = TwitterAuthForm()
if form.validate_on_submit():
#this is where my error is happening
auth.set_consumer_key(form.consumer_key.data)
auth.set_consumer_secret(form.consumer_secret.data)
auth.set_access_token(form.access_token.data)
auth.set_access_secret(form.access_secret.data)
return redirect('/index.html')
return render_template('authenticate.html',
title='Sign In',
form=form)
我的 TwitterAuth() 实现只是 getter 和 setter
class TwitterAuth():
consumer_key = ""
consumer_secret = ""
access_token = ""
access_secret = ""
def set_consumer_key(ck):
consumer_key = ck
def set_consumer_secret(cs):
consumer_secret = cs
def set_access_token(at):
access_token = at
def set_access_secret(access_sec):
access_secret = access_sec
def get_consumer_key():
return consumer_key
def get_consumer_secret():
return consumer_secret
def get_access_token():
return access_token
def get_access_secret():
return access_secret
TwitterAuth class 中的 None 方法有一个 self
参数,这就是你得到错误的原因。
在Python中,每个实例方法的第一个参数必须是self
。
(其实可以叫任何名字,但self
是约定俗成的名字:你不会看到任何其他名字被使用)。
编辑:
此外,在您的 getter 和 setter 方法中,您应该 return self.attribute_name
并分配给 self.attribute_name
这样您就 return修改并设置 TwitterAuth
实例的属性值,否则会遇到更多错误。
我正在尝试使用 form.consumer_key.data
从我的 WTForm 传递数据,但我收到一个参数,说我正在传递两个参数。我做错了什么?
这是我的错误
File "/Users/Gaby/Documents/Code/Twitty/app/views.py", line 18, in authenticate
这是我尝试传递文件的路径
auth = TwitterAuth()
@app.route('/')
@app.route('/authenticate', methods=['GET', 'POST'])
def authenticate():
form = TwitterAuthForm()
if form.validate_on_submit():
#this is where my error is happening
auth.set_consumer_key(form.consumer_key.data)
auth.set_consumer_secret(form.consumer_secret.data)
auth.set_access_token(form.access_token.data)
auth.set_access_secret(form.access_secret.data)
return redirect('/index.html')
return render_template('authenticate.html',
title='Sign In',
form=form)
我的 TwitterAuth() 实现只是 getter 和 setter
class TwitterAuth():
consumer_key = ""
consumer_secret = ""
access_token = ""
access_secret = ""
def set_consumer_key(ck):
consumer_key = ck
def set_consumer_secret(cs):
consumer_secret = cs
def set_access_token(at):
access_token = at
def set_access_secret(access_sec):
access_secret = access_sec
def get_consumer_key():
return consumer_key
def get_consumer_secret():
return consumer_secret
def get_access_token():
return access_token
def get_access_secret():
return access_secret
None 方法有一个 self
参数,这就是你得到错误的原因。
在Python中,每个实例方法的第一个参数必须是self
。
(其实可以叫任何名字,但self
是约定俗成的名字:你不会看到任何其他名字被使用)。
编辑:
此外,在您的 getter 和 setter 方法中,您应该 return self.attribute_name
并分配给 self.attribute_name
这样您就 return修改并设置 TwitterAuth
实例的属性值,否则会遇到更多错误。