如何从PythonFlask中的主页(index.html)链接到使用Flask admin的管理部分?

如何从PythonFlask中的主页(index.html)链接到使用Flask admin的管理部分?,flask,flask-admin,Flask,Flask Admin,我正在第一个项目中使用flask。我需要用什么方法从网站主页添加到管理部分的链接?如果您询问如何保护此链接,则取决于您如何处理用户帐户。我通常使用和用户模型上的一个方法执行此操作,如果用户是管理员,该方法将返回True,请参见以下代码段: {% if current_user.is_admin() %} <a href="/admin" style="color:red">Admin</a> {% endif %} {%如果当前用户为_admin()%} {%endi

我正在第一个项目中使用flask。我需要用什么方法从网站主页添加到管理部分的链接?

如果您询问如何保护此链接,则取决于您如何处理用户帐户。我通常使用和用户模型上的一个方法执行此操作,如果用户是管理员,该方法将返回True,请参见以下代码段:

{% if current_user.is_admin() %}
 <a href="/admin" style="color:red">Admin</a>
{% endif %}
{%如果当前用户为_admin()%}
{%endif%}

当前用户通过Flask登录传递到模板。

如果您询问如何保护此链接,则取决于您如何处理用户帐户。我通常使用和用户模型上的一个方法执行此操作,如果用户是管理员,该方法将返回True,请参见以下代码段:

{% if current_user.is_admin() %}
 <a href="/admin" style="color:red">Admin</a>
{% endif %}
{%如果当前用户为_admin()%}
{%endif%}
当前用户通过Flask登录传递到模板。

是/admin。您还可以使用('admin.index')的
url\u获取默认路由。

注意,每个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导入flask,url,呈现模板字符串
从flask_admin导入admin
app=烧瓶(名称)
默认值\u admin=admin()
默认\u admin.init\u应用程序(应用程序)
admin_1=admin(endpoint=“other”,url=“/other”)
管理员1.初始化应用程序(应用程序)
admin_2=admin(endpoint=“this_is_long_endpoint”,url=“/this_is_long_url”)
管理员2.初始化应用程序(应用程序)
admin_3=admin(endpoint=“test”,url=“/test/test”)
管理员3.初始化应用程序(应用程序)
#admin_异常_1=admin()
#管理异常1.初始化应用程序(应用程序)
#这与默认的_admin具有相同的端点-不允许
#不能有两个端点名称相同的Admin()实例。
#admin\u exception\u 2=admin(endpoint=“admin1”,url=“/admin”)
#管理异常2.初始化应用程序(应用程序)
#此url与默认的_admin相同-不允许
#无法将具有相同URL和子域的两个Admin()实例分配给同一应用程序。
index_template=“”
统一资源定位地址
端点
{%用于链接中的链接%}
{{link.url}
{{link.endpoint}}
{%endfor%}
"""
@应用程序路径(“/”)
def index():
_链接=[]
_endpoints=['admin.index','other.index','this_是一个_long_endpoint.index','test.index']
对于_端点中的_端点:
_links.append(
{
“url”:用于(_端点)的url_,
“端点”:
}
)
返回渲染模板字符串(索引模板,链接=\u链接)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
运行(端口=7000,调试=True)
样本输出

是/admin。您还可以使用('admin.index')的
url\u获取默认路由。

注意,每个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导入flask,url,呈现模板字符串
从flask_admin导入admin
app=烧瓶(名称)
默认值\u admin=admin()
默认\u admin.init\u应用程序(应用程序)
admin_1=admin(endpoint=“other”,url=“/other”)
管理员1.初始化应用程序(应用程序)
admin_2=admin(endpoint=“this_is_long_endpoint”,url=“/this_is_long_url”)
管理员2.初始化应用程序(应用程序)
admin_3=admin(endpoint=“test”,url=“/test/test”)
管理员3.初始化应用程序(应用程序)
#admin_异常_1=admin()
#管理异常1.初始化应用程序(应用程序)
#这与默认的_admin具有相同的端点-不允许
#不能有两个端点名称相同的Admin()实例。
#admin\u exception\u 2=admin(endpoint=“admin1”,url=“/admin”)
#管理异常2.初始化应用程序(应用程序)
#此url与默认的_admin相同-不允许
#无法将具有相同URL和子域的两个Admin()实例分配给同一应用程序。
index_template=“”
统一资源定位地址
端点
{%用于链接中的链接%}
{{link.url}
{{link.endpoint}}
{%endfor%}
"""
@应用程序路径(“/”)
def index():
_链接=[]
_endpoints=['admin.index','other.index','this_是一个_long_endpoint.index','test.index']
对于_端点中的_端点:
_links.append(
{
“url”:用于(_端点)的url_,
“端点”:
}
)
返回渲染模板字符串(索引模板,链接=\u链接)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
运行(端口=7000,调试=True)
样本输出