Search code examples
pythonflaskflask-admin

Flask-Admin - custom view with original form


I am trying to create a custom edit-view in Flask-Admin where I use the original edit form and add more content to it.

I want to use in my template the original:

{% extends 'admin/master.html' %}
{% import 'admin/lib.html' as lib with context %}

(...)

{% block edit_form %}
    {{ lib.render_form(form, return_url, extra(), form_opts) }}
{% endblock %}

But I am having trouble passing the original form to the self.render(...) function in my custom view:

class MyView(sqla.ModelView):
    # (view options)

    @expose('/edit/', methods=('GET', 'POST'))
    def edit_view(self):
        # (custom view variables)

        return self.render('my_edit_view.html')

I believe I have to pass some parameters in the self.render function. I tried to include a form = form but it didn't work (TypeError: 'module' object is not iterable)...

Any ideas?

Thanks!!


Solution

  • Figured it out thanks to this answer: https://stackoverflow.com/a/20714237/7811624

    I believe the correct way to use the original form plus extra fields in a custom view is:

    class MyView(sqla.ModelView):
        # (view options)
        edit_template = 'my_edit_view.html'
    
        @expose('/edit/', methods=('GET', 'POST'))
        def edit_view(self):
            # (custom view variables)
            # for the extra variables you wanna pass on to the template:
            self._template_args['foo'] = bar 
    
            return super(MyView, self).edit_view()