I want to implement an anchor to a SubmitField that is a member of Flask WTForms. Therefore, I override the SubmitField class and try to instantiate it, but always fail with an error no matter what I try. Here's my custom SubmitField class overriding the default SubmitField:
class SubmitFieldWithAnchor(SubmitField):
anchor_url = None
def __init__(self, label, **kwargs):
self.anchor_url = kwargs.pop('anchor_url')
super().__init__(label=label, render_kw=kwargs)
def __call__(self, **kwargs):
kwargs.setdefault('id', self.id)
html = super(SubmitFieldWithAnchor, self).__call__(**kwargs)
render_kw = kwargs.pop('render_kw', None)
if self.anchor_url:
html = '<a href="%s">%s</a>' % (self.anchor_url, html)
if render_kw:
html_attrs = ' '.join(['%s="%s"' % (k, v) for k, v in render_kw.items()])
html = '<button %s>%s</button>' % (html_attrs, html)
return html
Here's how I call it:
add = SubmitFieldWithAnchor('add after', render_kw={'class': 'add_btn', 'anchor_url': 'test.de'})
When running the app I get this not very speaking error:
Message: "'anchor_url'"
Arguments: ()
I also tried to hand over the 'anchor_url' outside of the 'render_kw' but that leads to some different exception:
Message: 'Must provide one of _form or _meta'
Any hints or suggestions? Thanks a lot!
EDIT:
I wanted to implement the anchor as I wanted to be able to hop back to the part of the page where the SubmitField sits after clicking on it after which the page reloads. Is this possible? I'm not sure anymore, or maybe it's possible to prevent the page from reloading as I only want to do some JavaScript 'magic' when clicking.