[英]Flask-Admin and Blueprint factory pattern is giving werkzeug.routing.BuildError: Could not build url for endpoint 'admin.static'
In Flask-Admin Blueprint factory pattern, I'm getting following error when I try to visit http://127.0.0.1:5000/users/ here is given the complete stack trace of the application when I try to open the endpoint in browser 在Flask-Admin Blueprint工厂模式中,当我尝试访问http://127.0.0.1:5000/users/时出现以下错误当我尝试在浏览器中打开端点时,给出了应用程序的完整堆栈跟踪
File "/home/maverick/.local/lib/python3.5/site-packages/flask_admin/base.py", line 308, in render
return render_template(template, **kwargs)
File "/home/maverick/.local/lib/python3.5/site-packages/flask/templating.py", line 135, in render_template
context, ctx.app)
File "/home/maverick/.local/lib/python3.5/site-packages/flask/templating.py", line 117, in _render
rv = template.render(context)
File "/home/maverick/.local/lib/python3.5/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/home/maverick/.local/lib/python3.5/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/maverick/.local/lib/python3.5/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/home/maverick/.local/lib/python3.5/site-packages/flask_admin/templates/bootstrap3/admin/model/list.html", line 6, in top-level template code
{% import 'admin/model/row_actions.html' as row_actions with context %}
File "/home/maverick/.local/lib/python3.5/site-packages/flask_admin/templates/bootstrap3/admin/master.html", line 1, in top-level template code
{% extends admin_base_template %}
File "/home/maverick/.local/lib/python3.5/site-packages/flask_admin/templates/bootstrap3/admin/base.html", line 14, in top-level template code
{% block head_css %}
File "/home/maverick/.local/lib/python3.5/site-packages/flask_admin/templates/bootstrap3/admin/base.html", line 15, in block "head_css"
<link href="{{ admin_static.url(filename='bootstrap/bootstrap3/swatch/{swatch}/bootstrap.min.css'.format(swatch=config.get('FLASK_ADMIN_SWATCH', 'default')), v='3.3.5') }}" rel="stylesheet">
File "/home/maverick/.local/lib/python3.5/site-packages/jinja2/runtime.py", line 579, in _invoke
rv = self._func(*arguments)
File "/home/maverick/.local/lib/python3.5/site-packages/flask_admin/templates/bootstrap3/admin/static.html", line 2, in template
{{ get_url('admin.static', *varargs, **kwargs) }}
File "/home/maverick/.local/lib/python3.5/site-packages/flask_admin/base.py", line 390, in get_url
return url_for(endpoint, **kwargs)
File "/home/maverick/.local/lib/python3.5/site-packages/flask/helpers.py", line 356, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "/home/maverick/.local/lib/python3.5/site-packages/flask/app.py", line 2061, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "/home/maverick/.local/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/maverick/.local/lib/python3.5/site-packages/flask/helpers.py", line 345, in url_for
force_external=external)
File "/home/maverick/.local/lib/python3.5/site-packages/werkzeug/routing.py", line 2181, in build
raise BuildError(endpoint, values, method, self)
werkzeug.routing.BuildError: Could not build url for endpoint 'admin.static' with values ['filename', 'v']. Did you mean 'static' instead?
app/__init__.py 应用程序/ __ init__.py
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)
# extension initiation
[...]
# factory registration
[...]
from app.admin.core import MicroModelView # subclass of ModelView
from app.models import User # db table
f_admin = Admin(
app,
name='new admin',
index_view=MicroModelView(User, db.session, endpoint='users', url='/users'),
template_mode='bootstrap3',
)
app/admin/core.py: 应用程序/管理/ core.py:
class MicroModelView(ModelView):
page_size = 5
What is the problem, and how can it be resolved? 问题是什么,如何解决?
The error is caused by you setting index_view
to a ModelView
subclass. 该错误是由你设置引起
index_view
到ModelView
子类。 Don't do that. 不要那样做。
Normally, index_view
is set to an instance of the AdminIndexView()
class , and it is this class that registers an admin.static
view to handle static files used in the admin UI (the Javascript and CSS files used in the templates). 通常,
index_view
设置为AdminIndexView()
类的实例,并且此类注册admin.static
视图以处理管理UI中使用的静态文件(模板中使用的Javascript和CSS文件)。
But a ModelView
subclass does not provide those. 但
ModelView
子类不提供这些。 You need to register those with admin.add_view()
calls instead: 您需要使用
admin.add_view()
调用注册:
f_admin = Admin(
app,
name='new admin',
template_mode='bootstrap3',
)
f_admin.add_view(MicroModelView(User, db.session, endpoint='users', url='/users'))
If you want to change the default /admin/
page behaviour, you'd need to subclass the AdminIndexView
class and override its index
method; 如果要更改默认
/admin/
page行为,则需要AdminIndexView
类并覆盖其index
方法; you could redirect to the users
view, for instance: 您可以重定向到
users
视图,例如:
# additional imports
from flask import redirect
from flask_admin import AdminIndexView, expose
class MicroModelAdminIndexView(AdminIndexView):
@expose
def index(self):
return redirect('users.index')
f_admin = Admin(
app,
name='new admin',
template_mode='bootstrap3',
index_view=MicroModelAdminIndexView()
)
f_admin.add_view(MicroModelView(User, db.session, endpoint='users', url='/users'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.