简体   繁体   中英

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.

在此处输入图片说明

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 %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM