Passing Arguments to ModelView edit template in flask-admin

我只是一个虾纸丫 提交于 2019-12-04 17:40:46

问题


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?


回答1:


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')


来源:https://stackoverflow.com/questions/20709255/passing-arguments-to-modelview-edit-template-in-flask-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!