将参数传递给flask-admin中的ModelView编辑模板

Sum*_*mit 12 python flask flask-admin

我想通过构建CMS来了解有关Flask的更多信息.我正在使用flask-admin添加帖子,图片等.

我设法用ckeditor覆盖textarea .但我想将静态文件夹中图像的路径传递给ckeditor图像插件.

我无法弄清楚如何将参数传递给我的edit.html模板.

这是代码:

class TestAdmin(ModelView):
    form_overrides = dict(text=forms.CustomTextAreaField)
    create_template = 'edit.html'
    edit_template = 'edit.html'
Run Code Online (Sandbox Code Playgroud)

从flask-admin的文档中我发现_template_args可以用来将参数传递给模板.但我无法弄清楚如何.

这样做的确切方法是什么?

iur*_*vio 17

您必须覆盖要更改的视图_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()
Run Code Online (Sandbox Code Playgroud)

如果要将某些全局值传递给模板,可以使用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')
Run Code Online (Sandbox Code Playgroud)

  • 我正在尝试用 index_view 做一些类似的事情,但它根本不起作用。我不断收到异常:异常:尝试在没有默认视图的情况下实例化管理视图 UserModelView 知道出了什么问题吗? (2认同)