0

我想做这样的事情,但不知道是否有可能:

    from flask.ext.wtf import Form, TextField
    class Foo(Form):
        a = TextField('name')
    class Bar(Form):
        b = Foo()

我得到的错误是

   Traceback (most recent call last):
   File "data_attr.py", line 4, in <module>
     class Bar(Form):
   File "data_attr.py", line 5, in Bar
     b = Foo()
   File "/usr/local/lib/python2.7/dist-packages/wtforms/form.py", line 178, in __call__
     return type.__call__(cls, *args, **kwargs)
   File "/usr/local/lib/python2.7/dist-packages/flask_wtf/form.py", line 44, in __init__
     csrf_enabled = current_app.config.get('CSRF_ENABLED', True)
   File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 336, in __getattr__
     return getattr(self._get_current_object(), name)
   File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 295, in _get_current_object
     return self.__local()
   File "/usr/local/lib/python2.7/dist-packages/flask/globals.py", line 26, in _find_app
     raise RuntimeError('working outside of application context')
   RuntimeError: working outside of application context

还有其他方法吗?谢谢!

4

1 回答 1

2

您可以通过FormField类使用 Field Enclosure

class Foo(Form):
    a = TextField('name')

class Bar(Form):
    b = FormField(Foo)

http://wtforms.simplecodes.com/docs/1.0.3/fields.html#field-enclosures

于 2013-04-08T18:53:34.163 回答