Passing Arguments to ModelView edit template in flask-admin
Asked Answered
R

2

18

I am trying to learn more about Flask by building a CMS. I am using flask-admin to add the posts, images etc.

I have managed to override textarea with ckeditor. But I want to pass the paths of the images in the static folder to ckeditor image plugin.

I can't figure out how to pass parameters to my edit.html template.

Here's the code:

class TestAdmin(ModelView):
    form_overrides = dict(text=forms.CustomTextAreaField)
    create_template = 'edit.html'
    edit_template = 'edit.html'

From the documentation of flask-admin I have found that _template_args can used to pass parameters to the template. But I can't figure out how.

What is the exact way to do that?

Ranch answered 20/12, 2013 at 17:49 Comment(0)
S
22

You have to override the views to change _template_args.

class TestAdmin(ModelView):
    form_overrides = dict(text=forms.CustomTextAreaField)
    create_template = 'edit.html'
    edit_template = 'edit.html'

    @expose('/edit/', methods=('GET', 'POST'))
    def edit_view(self):
         self._template_args['foo'] = 'bar'
         return super(TestAdmin, self).edit_view()

If you want to pass some global value to templates, you can use a context_processor (http://flask.pocoo.org/docs/templating/#context-processors).

@app.context_processor
def inject_paths():
    # you will be able to access {{ path1 }} and {{ path2 }} in templates
    return dict(path1='x', path2='y')
Santoyo answered 21/12, 2013 at 0:55 Comment(7)
I'm trying to do something similar with index_view but it's not working at all. I keep getting the exception: Exception: Attempted to instantiate admin view UserModelView without default view Any idea what's wrong?Myrmidon
@JamieHush no idea, I can't help without some code. It should work if you subclass AdminIndexView and override index function.Santoyo
AttributeError: 'super' object has no attribute 'index'Dryly
@Dryly This answer is a bit old, maybe Flask-Admin changed some implementation detail. I really don't know why this code calls index() instead of edit_view(). I'll update my answer.Santoyo
I guess super(TestAdmin, self).edit_view() should be returned, too.Dryly
@JamesHush you may forget to have a @expose at your index(self). this is how i got the same error as you didPriapism
The context processor method with global variables works well for me. I couldn't get the list view to work with template args. CheersInlaw
B
2

To anyone finding this in 2023+ – you can do it now by overriding _get_list_extra_args method:

class YourView(ModelView):
    list_template = "custom_template.html"

    def _get_list_extra_args(self):
        view_args = super()._get_list_extra_args()
        view_args.extra_args["foo"] = "bar"
        return view_args

Here we can control extra_args dict. Flask-Admin's index_view() passes it straight to the template as is, so you can modify it and use it in custom_template.html:

<div>
    {{ extra_args.foo }}  {# will render "bar" #}
</div>
Bairam answered 30/1, 2023 at 0:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.