您如何从 python Flask 中的主页 (index.html) link 转到使用 Flask-admin 的管理部分?
How do you link from a main page (index.html) in python Flask to the admin section which is using Flask-admin?
我在第一个项目中使用 Flask。我需要用什么方法从站点的主页向管理部分添加 link?
如果您要问如何保护这个 link 它取决于您处理用户帐户的方式。我通常使用 Flask-Login 和用户模型上的方法 returns 如果用户是管理员则为真,请参阅此代码段:
{% if current_user.is_admin() %}
<a href="/admin" style="color:red">Admin</a>
{% endif %}
Flask-Login 传递给模板的 current_user。
default URL route 是 /admin。您还可以使用 url_for('admin.index')
.
获取默认路由
请注意,每个 Flask 应用程序可以有多个 flask-admin 实例。请参阅下面说明这一点的独立代码段。
from flask import Flask, url_for, render_template_string
from flask_admin import Admin
app = Flask(__name__)
default_admin = Admin()
default_admin.init_app(app)
admin_1 = Admin(endpoint="another", url="/another")
admin_1.init_app(app)
admin_2 = Admin(endpoint="this_is_a_long_endpoint", url="/this_is_a_long_url")
admin_2.init_app(app)
admin_3 = Admin(endpoint="test", url="/test/test")
admin_3.init_app(app)
# admin_exception_1 = Admin()
# admin_exception_1.init_app(app)
# This has the same endpoint as default_admin - not allowed
# Cannot have two Admin() instances with same endpoint name.
# admin_exception_2 = Admin(endpoint="admin1", url="/admin")
# admin_exception_2.init_app(app)
# This has the same url as default_admin - not allowed
# Cannot assign two Admin() instances with same URL and subdomain to the same application.
index_template = """
<table>
<thead>
<tr>
<th>URL</th>
<th>Endpoint</th>
</tr>
</thead>
<tbody>
{% for link in links %}
<tr>
<td>{{ link.url }}</td>
<td>{{ link.endpoint }}</td>
</tr>
{% endfor %}
</tbody>
</table>
"""
@app.route('/')
def index():
_links = []
_endpoints = ['admin.index', 'another.index', 'this_is_a_long_endpoint.index', 'test.index']
for _endpoint in _endpoints:
_links.append(
{
'url': url_for(_endpoint),
'endpoint': _endpoint
}
)
return render_template_string(index_template, links=_links)
if __name__ == '__main__':
app.run(port=7000, debug=True)
示例输出
我在第一个项目中使用 Flask。我需要用什么方法从站点的主页向管理部分添加 link?
如果您要问如何保护这个 link 它取决于您处理用户帐户的方式。我通常使用 Flask-Login 和用户模型上的方法 returns 如果用户是管理员则为真,请参阅此代码段:
{% if current_user.is_admin() %}
<a href="/admin" style="color:red">Admin</a>
{% endif %}
Flask-Login 传递给模板的 current_user。
default URL route 是 /admin。您还可以使用 url_for('admin.index')
.
请注意,每个 Flask 应用程序可以有多个 flask-admin 实例。请参阅下面说明这一点的独立代码段。
from flask import Flask, url_for, render_template_string
from flask_admin import Admin
app = Flask(__name__)
default_admin = Admin()
default_admin.init_app(app)
admin_1 = Admin(endpoint="another", url="/another")
admin_1.init_app(app)
admin_2 = Admin(endpoint="this_is_a_long_endpoint", url="/this_is_a_long_url")
admin_2.init_app(app)
admin_3 = Admin(endpoint="test", url="/test/test")
admin_3.init_app(app)
# admin_exception_1 = Admin()
# admin_exception_1.init_app(app)
# This has the same endpoint as default_admin - not allowed
# Cannot have two Admin() instances with same endpoint name.
# admin_exception_2 = Admin(endpoint="admin1", url="/admin")
# admin_exception_2.init_app(app)
# This has the same url as default_admin - not allowed
# Cannot assign two Admin() instances with same URL and subdomain to the same application.
index_template = """
<table>
<thead>
<tr>
<th>URL</th>
<th>Endpoint</th>
</tr>
</thead>
<tbody>
{% for link in links %}
<tr>
<td>{{ link.url }}</td>
<td>{{ link.endpoint }}</td>
</tr>
{% endfor %}
</tbody>
</table>
"""
@app.route('/')
def index():
_links = []
_endpoints = ['admin.index', 'another.index', 'this_is_a_long_endpoint.index', 'test.index']
for _endpoint in _endpoints:
_links.append(
{
'url': url_for(_endpoint),
'endpoint': _endpoint
}
)
return render_template_string(index_template, links=_links)
if __name__ == '__main__':
app.run(port=7000, debug=True)
示例输出