Flask从数据库设置表单默认值

时间:2016-07-20 08:03:31

标签: flask

我正在尝试一个简单的烧瓶形式,这里是代码:

class settingsForm(Form):
    def get_new_login_enable_sms():
        user = current_user
        print user.get_id()
        return True
    new_login_enable_sms = BooleanField('new_login_enable_sms',default=get_new_login_enable_sms())

我想做的是,不是通过函数返回True或False,我想从数据库中获取这些值,为此,我需要检查谁是当前用户,这个类是在一个定义的单独的文件比视图和烧瓶方法current_user不起作用,我得到的是以下错误:

Traceback (most recent call last):
  File "views.py", line 9, in <module>
    from forms import *
  File "/home/taha/Desktop/rasberry_pi_project/Flask_rasberrypi_project/app/forms.py", line 15, in <module>
    class settingsForm(Form):
  File "/home/taha/Desktop/rasberry_pi_project/Flask_rasberrypi_project/app/forms.py", line 40, in settingsForm
    new_login_enable_sms              = BooleanField('new_login_enable_sms',default=get_new_login_enable_sms())
  File "/home/taha/Desktop/rasberry_pi_project/Flask_rasberrypi_project/app/forms.py", line 18, in get_new_login_enable_sms
    print user.get_id()
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 343, in __getattr__
    return getattr(self._get_current_object(), name)
AttributeError: 'NoneType' object has no attribute 'get_id'

这是我的Loader功能:

@login_manager.user_loader def load_user(id): conn = sqlite3.connect('users.db') c=conn.cursor() # fetch user_name: query="SELECT user_name FROM user WHERE id = '"+id+"'" c.execute(query) user_name = c.fetchone()[0] # fetch is_authenticated: query="SELECT is_authenticated FROM user WHERE id = '"+id+"'" c.execute(query) is_authenticated = c.fetchone()[0] # fetch is_active: query="SELECT is_active FROM user WHERE id = '"+id+"'" c.execute(query) is_active = c.fetchone()[0] #close connection: conn.close() user= User(user_name,is_active,is_authenticated) return user

0 个答案:

没有答案