diff --git a/examples/forms-files-images/app.py b/examples/forms-files-images/app.py index 138515557..325b68635 100644 --- a/examples/forms-files-images/app.py +++ b/examples/forms-files-images/app.py @@ -172,6 +172,8 @@ class UserView(sqla.ModelView): """ This class demonstrates the use of 'rules' for controlling the rendering of forms. """ + can_view_details = True + form_create_rules = [ # Header and four fields. Email field will go above phone field. rules.FieldSet(('first_name', 'last_name', 'email', 'phone', 'is_admin'), 'Personal'), @@ -206,6 +208,7 @@ class UserView(sqla.ModelView): create_template = 'create_user.html' edit_template = 'edit_user.html' + details_template = 'details_user.html' column_descriptions = { "is_admin": "Is this an admin user?", diff --git a/examples/forms-files-images/templates/details_user.html b/examples/forms-files-images/templates/details_user.html new file mode 100644 index 000000000..9290ef777 --- /dev/null +++ b/examples/forms-files-images/templates/details_user.html @@ -0,0 +1,2 @@ +{% extends 'admin/model/details.html' %} +{% import 'macros.html' as rule_demo %} diff --git a/flask_admin/model/base.py b/flask_admin/model/base.py index 665e64680..14475eeea 100755 --- a/flask_admin/model/base.py +++ b/flask_admin/model/base.py @@ -2169,7 +2169,8 @@ def edit_view(self): @expose('/details/') def details_view(self): """ - Details model view + Details model view, which looks like the "create" and "edit" views, + but with read-only fields. """ return_url = get_redirect_target() or self.get_url('.index_view') @@ -2186,6 +2187,22 @@ def details_view(self): flash(gettext('Record does not exist.'), 'error') return redirect(return_url) + # Send the form to the template, same as the "edit" view + form = self.edit_form(obj=model) + if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset: + self._validate_form_instance(ruleset=self._form_edit_rules, form=form) + + self.on_form_prefill(form, id) + + # Make the input fields "read-only" since this isn't the "edit" view + widget_args = {} + for field in self._form_edit_rules.visible_fields: + widget_args[field] = self.form_widget_args.get(field, {}) + widget_args[field]['readonly'] = True + + form_opts = FormOpts(widget_args=widget_args, + form_rules=self._form_edit_rules) + if self.details_modal and request.args.get('modal'): template = self.details_modal_template else: @@ -2193,6 +2210,8 @@ def details_view(self): return self.render(template, model=model, + form=form, + form_opts=form_opts, details_columns=self._details_columns, get_value=self.get_detail_value, return_url=return_url) diff --git a/flask_admin/templates/bootstrap4/admin/lib.html b/flask_admin/templates/bootstrap4/admin/lib.html index b8395d1f5..87dac0ae6 100644 --- a/flask_admin/templates/bootstrap4/admin/lib.html +++ b/flask_admin/templates/bootstrap4/admin/lib.html @@ -236,10 +236,13 @@

{{ text }}

{% endif %} {% endmacro %} -{% macro render_form(form, cancel_url, extra=None, form_opts=None, action=None, is_modal=False) -%} +{% macro render_form(form, cancel_url, extra=None, form_opts=None, action=None, is_modal=False, render_buttons=True) -%} {% call form_tag(action=action) %} {{ render_form_fields(form, form_opts=form_opts) }} - {{ render_form_buttons(cancel_url, extra, is_modal) }} + {# The "details" view doesn't need or want "Save" and "Cancel" buttons #} + {% if render_buttons %} + {{ render_form_buttons(cancel_url, extra, is_modal) }} + {% endif %} {% endcall %} {% endmacro %} diff --git a/flask_admin/templates/bootstrap4/admin/model/details.html b/flask_admin/templates/bootstrap4/admin/model/details.html index f4017e3df..2c1f62588 100644 --- a/flask_admin/templates/bootstrap4/admin/model/details.html +++ b/flask_admin/templates/bootstrap4/admin/model/details.html @@ -1,5 +1,11 @@ {% extends 'admin/master.html' %} {% import 'admin/lib.html' as lib with context %} +{% from 'admin/lib.html' import extra with context %} {# backward compatible #} + +{% block head %} + {{ super() }} + {{ lib.form_css() }} +{% endblock %} {% block body %} {% block navlinks %} @@ -23,30 +29,12 @@ {% endblock %} - {% block details_search %} -
- - -
- {% endblock %} - - {% block details_table %} - - {% for c, name in details_columns %} - - - - - {% endfor %} -
- {{ name }} - - {{ get_value(model, c) }} -
+ {% block edit_form %} + {{ lib.render_form(form, return_url, extra=None, form_opts=form_opts, render_buttons=False, input_disable=True) }} {% endblock %} {% endblock %} {% block tail %} {{ super() }} - + {{ lib.form_js() }} {% endblock %}