Using split in my flask wtf form

hi all,

im trying to put a split command in my code like so

class PasswordForm(FlaskForm):
    un = StringField('Username', [InputRequired(message='please enter your Username')])
    name = un.split(".")
    op = PasswordField('Current Password', [InputRequired(message='please enter your current password')])
    np = PasswordField('New Password', [InputRequired(message='please enter your new password'), EqualTo('cnp', message='must match confirm new password'), Length(min=12), Regexp('.*[a-z]', message='must contain one lower case'), Regexp('.*[A-Z]', message='must contain one upper case'), Regexp('.*[0-9]', message='must contain one number'), Regexp('.*[\¬\!\"\£\$\%\^\&\*\(\)\_\+\`\-\=\{\}\:\@\~\<\>\?\[\]\;\'\#\,\.\/\\\|]', message='must contain one special character')])
    cnp = PasswordField('Confirm New Password')

but i get this error when i run it in cmd

C:\python>python password.py
C:\python\password.py:14: SyntaxWarning: invalid escape sequence '\!'
  np = PasswordField('New Password', [InputRequired(message='please enter your new password'), EqualTo('cnp', message='must match confirm new password'), Length(min=12), Regexp('.*[a-z]', message='must contain one lower case'), Regexp('.*[A-Z]', message='must contain one upper case'), Regexp('.*[0-9]', message='must contain one number'), Regexp('.*[\¬\!\"\£\$\%\^\&\*\(\)\_\+\`\-\=\{\}\:\@\~\<\>\?\[\]\;\'\#\,\.\/\\\|]', message='must contain one special character')])
Traceback (most recent call last):
  File "C:\python\password.py", line 10, in <module>
    class PasswordForm(FlaskForm):
  File "C:\python\password.py", line 12, in PasswordForm
    name = un.split(".")
           ^^^^^^^^
AttributeError: 'UnboundField' object has no attribute 'split'

any help will be greatly appreciated

thanks,
rob

Look at the previous line in the code. You’re making un a StringField object. str objects have a split method, but StringField objects don’t.

so basically i cant use it with the un variable as its a stringfield?

sorry as im fairly new to python

form.un is the field object. form.un.data is the string, which you would access after creating an instance of your form to process submitted data.

tried

name = form.un.data.split(“.”)
name = form.un.split(“.”)

same error saying form is not defined

form = MyForm()

It sounds like you may not be familiar with WTForms and Flask-WTF. Manipulating data is done after creating an instance of the form, not when defining the class and fields. You should start with their docs first: Crash Course — WTForms Documentation (3.1.x) and Quickstart — Flask-WTF Documentation (1.2.x)