Formatting flask-wtf submit button using bootstrap
Asked Answered
S

4

6

I'm using jinja to render a flask-wtf submit button as follows:

{{ wtf.form_field(form.submit) }}

This results in a button formatted in btn-default bootstrap format (white). I'd like to change this to btn-success bootstrap format (green).

How can I achieve this?

Snips answered 25/5, 2015 at 12:12 Comment(0)
R
3

I presume you are also using flask-bootstrap.

On the flask-bootstrap Jinja2 macros you have:

    {% call _hz_form_wrap(horizontal_columns, form_type, True, required=required) %}
    {{field(class='btn btn-%s' % button_map.get(field.name, 'default'), **kwargs)}}
    {% endcall %}

You should use if you can the button_map to do it [see details in comments below]

Repudiation answered 25/5, 2015 at 14:46 Comment(2)
I am indeed also using flask-bootstrap. I'm afraid I do not follow your suggestion though. Can you elaborate a bit further please?Snips
please see if this helps on the docs: pythonhosted.org/Flask-Bootstrap/basic-usage.html#quick_form or pythonhosted.org/Flask-Bootstrap/basic-usage.html#form_fieldRepudiation
S
10

As suggested by @dpgaspar the solution was to use button_map as follows:

{{ wtf.form_field(form.submit, button_map={'submit':'success'}) }}
Snips answered 27/5, 2015 at 6:19 Comment(2)
what is 'submit' referencing in the button map dict?Vivisectionist
correct me if i am wrong but it is the "name" attribute. i feel like flask-bootstrap docs are kind of weak here...Vivisectionist
S
9

If you are using wtf.quick_form, use the form like this.

{{ wtf.quick_form(form, button_map={'submit':'success'}) }}
Staford answered 24/6, 2016 at 23:36 Comment(0)
R
3

I presume you are also using flask-bootstrap.

On the flask-bootstrap Jinja2 macros you have:

    {% call _hz_form_wrap(horizontal_columns, form_type, True, required=required) %}
    {{field(class='btn btn-%s' % button_map.get(field.name, 'default'), **kwargs)}}
    {% endcall %}

You should use if you can the button_map to do it [see details in comments below]

Repudiation answered 25/5, 2015 at 14:46 Comment(2)
I am indeed also using flask-bootstrap. I'm afraid I do not follow your suggestion though. Can you elaborate a bit further please?Snips
please see if this helps on the docs: pythonhosted.org/Flask-Bootstrap/basic-usage.html#quick_form or pythonhosted.org/Flask-Bootstrap/basic-usage.html#form_fieldRepudiation
R
1

If you are using flask-bootstrap, use the form with button_map as suggested by @dpgaspar,

For whole form - wtf.quick_form:

{{wtf.quick_form(delete_form, button_map={'name_of_field': 'danger'})}}

For individual field, wtf.form_field:

{{wtf.form_field(delete_form.delete, button_map={'delete': 'success'})}}

Flask-Bootstrap official documentation says:

button_map – A dictionary, mapping button field names to names such as primary, danger or success. Buttons not found in the button_map will use the default type of button.

Rocambole answered 9/10, 2020 at 16:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.