Search code examples
pythonflaskflask-admin

flask-admin: revise the button text


I haven't found similar questions on stackoverflow, I'd like to change the save button to submit or confirm on the edit form. I know this might not be easily changed. Thanks for any advise in advance.

enter image description here


Solution

  • After search in the code of flask-admin, I found the button is rendered with macro render_form, render_form_buttons, extra. The value of these buttons is hard code with {{ _gettext("blabla") }}.

    As these buttons are not fields of data model, we can't use rendering rules to custom the value. I think there are two work arounds to get this done:

    • change the macro which render these buttons in the source of flask-admin(render_form_buttons, extra)
    • flask-admin use flask-babelex to do localization({{ _gettext("blabla") }}), you can 'translate' Save to submit or confirm with flask-babelex

    UPDATE:

    You can custom edit.html in your own template directory.

    {% extends 'admin/model/edit.html' %}
    {% from 'admin/lib.html' import extra with context %}
    {% from 'admin/lib.html' import form_tag with context %}
    {% from 'admin/lib.html' import render_form_fields with context %}
    
    {% macro my_render_form_buttons(cancel_url, extra=None, is_modal=False) %}
        <hr>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10 submit-row">
                <input type="submit" class="btn btn-primary" value="{{ _gettext('Submit') }}" />
                {% if extra %}
                {{ extra }}
                {% endif %}
                {% if cancel_url %}
                    <a href="{{ cancel_url }}" class="btn btn-danger" role="button" {% if is_modal %}data-dismiss="modal"{% endif %}>{{ _gettext('Cancel') }}</a>
                {% endif %}
             </div>
        </div>
    {% endmacro %}
    
    {% macro my_render_from(form, cancel_url, extra=None, form_opts=None, action=None, is_modal=False) -%}
        {% call form_tag(action=action) %}
            {{ render_form_fields(form, form_opts=form_opts) }}
            {{ my_render_form_buttons(cancel_url, extra, is_modal) }}
        {% endcall %}
    {% endmacro %}
    
    {% block edit_form %}
        {{ my_render_form(form, return_url, extra(), form_opts) }}
    {% endblock %}