Flask-Admin extending templates
Asked Answered
K

2

9

I'm trying to extend my template with 'master.html' template of Flask-Admin like this:

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

And I get error:

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

Here is how I'm initializing Flask-Admin:

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

And AdminView class:

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()
Kermitkermy answered 31/7, 2013 at 13:17 Comment(3)
Could you show how do you initialize your admin and template directory structure?Variety
Here you go, admin = Admin(app, name='Spiders') admin.add_view(AdminView(User, Session, name='Users'))Kermitkermy
How defined AdminView?Bullish
B
13

Look like you use template outside of Flask-Admin.

flask_admin.base.BaseView have self render function, which setup some variables. You can try setup it manualy, but however I do not see reason use Flask-Admin templates outside of Flask-Admin views.

See bad example:

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')
Bullish answered 2/8, 2013 at 7:33 Comment(2)
Oh. now I see. It was kind of stupid question :) Thanks a lot @BullishKermitkermy
@Bullish Since you seem to be a flask-admin pro: just in case you know the answer, I started a bounty on this question. :)Tuneberg
N
7

I ran into the same issue trying to extend the Flask-Admin templates.

Changing return render_template('path_to_template') to return self.render('path_to_template') resolved the issue.

Nicolella answered 8/1, 2014 at 17:52 Comment(1)
This works only if you have that self object (i.e. inside a class instance – in this case you not always can define @app.route() that easily).Barracks

© 2022 - 2024 — McMap. All rights reserved.