Flask-Admin扩展模板

时间:2013-07-31 13:17:33

标签: python flask jinja2 flask-admin

我正在尝试使用Flask-Admin的'master.html'模板扩展我的模板,如下所示:

{% extends 'admin/master.html' %}
{% block body %}
    Hello!!!
{% endblock %}

我得到错误:

File "/usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages/Jinja2-2.6-py2.7.egg/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "/Users/Slowpoke/Projects/Python/spider/spider/templates/form.html", line 1, in top-level template code
{% extends 'admin/master.html' %}
File "/usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages/Flask_Admin-1.0.6-py2.7.egg/flask_admin/templates/admin/master.html", line 1, in top-level template code
{% extends admin_base_template %}
File "/usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages/Flask-0.9-py2.7.egg/flask/templating.py", line 57, in get_source
return loader.get_source(environment, local_name)
File "/usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages/Jinja2-2.6-py2.7.egg/jinja2/loaders.py", line 162, in get_source
pieces = split_template_path(template)
File "/usr/local/Cellar/python/2.7.3/lib/python2.7/site-packages/Jinja2-2.6-py2.7.egg/jinja2/loaders.py", line 29, in split_template_path
for piece in template.split('/'):
UndefinedError: 'admin_base_template' is undefined

以下是我正在初始化Flask-Admin的方法:

admin = Admin(app, name='Spiders')
admin.add_view(AdminView(User, Session, name='Users'))

和AdminView类:

from flask.ext.admin.contrib.sqlamodel import ModelView
from flask.ext import login
class AdminView(ModelView):
    def is_accessible(self):
        return login.current_user.is_authenticated()

2 个答案:

答案 0 :(得分:8)

看起来您使用Flask-Admin之外的模板。

flask_admin.base.BaseView拥有自我render function,可设置一些变量。您可以尝试手动设置它,但是我没有看到原因在Flask-Admin视图之外使用Flask-Admin模板。

见坏例子:

class MyView(BaseView):
    def __init__(self, *args, **kwargs):
        self._default_view = True
        super(MyView, self).__init__(*args, **kwargs)
        self.admin = Admin()

@app.route('/form')
def form():
    return MyView().render('form.html')

答案 1 :(得分:7)

我遇到了同样的问题,试图扩展Flask-Admin模板。

return render_template('path_to_template')更改为return self.render('path_to_template')解决了这个问题。