Question

I'd like to change name attribure of SubmitField (which is "submit" by default). What have I tried:

from flask.ext.wtf import Form, SubmitField

class BaseForm(Form):
    submit = SubmitField('Create', id='submit_button', name='submit_button')  #1
    submit = SubmitField('Create', id='submit_button', _name='submit_button')  #2

    def __init__(self, edit=None, *args, **kwargs):
        self.submit.kwargs['name'] = 'submit_button'   #5
        self.submit.kwargs['_name'] = 'submit_button'   #6

All of them failed with different error. If I removing name or _name parameter all working fine. I found that name attribute is passed by flask.ext.wtf.Form but I have no sense how to fix it.

NOTE: I am using not trivial import of my form: it is imported in run-time, inside of view's method, not at the top of file. I cannot and will not change it because of technical issues. I.e. if I copy-pasting my code in IDLE it is working ok. But if I importing this code inside port method of MethodView it is fails.

Was it helpful?

Solution 2

Have you looked at extending the SubmitField itself with a custom constructor. See an example here

Basically you would do something like:

class CustomSubmitField(SubmitField):
    def __init__(self, label='', validators=None,_name='',**kwargs):
        super(SubmitField, self).__init__(label, validators, **kwargs)
        custom_name = "whatever"
        self._name = custom_name

OTHER TIPS

The simplest way to change the name is to change the name of the field:

class BaseForm(Form):
    # This one's name will be submit_button
    submit_button = SubmitField('Create')
    # This one's name will be another_button
    another_button = SubmitField('Do Stuff')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top