13

我正在尝试通过构建 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'

从 flask-admin 的文档中我发现_template_args可以用来将参数传递给模板。但我不知道怎么做。

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

4

1 回答 1

19

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

如果您想将一些全局值传递给模板,您可以使用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')
于 2013-12-21T00:55:52.157 回答