kvesteri/wtforms-components

'UnboundField' object has no attribute 'widget'

lov3catch opened this issue · 2 comments

In minimum example:

from flask import Flask
from wtforms import Form, DateField, TextField
from wtforms_components import TimeField, read_only

class EventForm(Form):
name = read_only(TextField('Name'))
start_date = DateField('Start date')
start_time = TimeField('Start time')

app = Flask(name)

@app.route('/')
def hello_world():
form = EventForm()
return 'Hello World!'

if name == 'main':
app.run()

Traceback:
/home/lov3catch/PycharmProjects/venv/Option/bin/python2.7 /home/lov3catch/PycharmProjects/wtf_test/wtf_test.py
Traceback (most recent call last):
File "/home/lov3catch/PycharmProjects/wtf_test/wtf_test.py", line 5, in
class EventForm(Form):
File "/home/lov3catch/PycharmProjects/wtf_test/wtf_test.py", line 6, in EventForm
name = read_only(TextField('Name'))
File "/home/lov3catch/PycharmProjects/venv/Option/local/lib/python2.7/site-packages/wtforms_components/init.py", line 83, in read_only
field.widget = ReadOnlyWidgetProxy(field.widget)
AttributeError: 'UnboundField' object has no attribute 'widget'

Pls, help

Try to move read_only to the route.

@app.route('/')
def hello_world():
    form = EventForm()
    read_only(form.name)
    return 'Hello World!'

or make a constructor for your form.

def __init__(self, *args, **kwargs):
    super(EventForm, self).__init__(*args, **kwargs)
    read_only(self.name)

Documentation seem to be outdated.

thank you