在flask-admin中将参数传递给ModelView编辑模板 [英] Passing Arguments to ModelView edit template in flask-admin

查看:83
本文介绍了在flask-admin中将参数传递给ModelView编辑模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

我不知道如何将参数传递给我的edit.html模板.

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

代码如下:

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

从flask-admin的文档中,我发现 _template_args 可用于将参数传递给模板.但是我不知道怎么办.

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.

确切的方法是什么?

推荐答案

您必须覆盖视图才能更改 _template_args .

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

如果要将某些全局值传递给模板,则可以使用 context_processor (

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

这篇关于在flask-admin中将参数传递给ModelView编辑模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆