FlaskAdmin中基于类的视图的url_

2024-06-28 20:07:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个基于类的管理视图:

class All_RDPs(BaseView):
    @expose('/')
    def index(self):
        return 'ok1'
    @expose('/test')
    def testindex(self):
        return 'ok2'

在Flask Admin注册如下:

^{pr2}$

然后可以从浏览器中查看,如下所示:

http://localhost/admin/all_rdps/
http://localhost/admin/all_rdps/test

问题是:

  1. 如何为这个类指定URL而不是默认生成的名称all_rdps?在
  2. 如何使用url_for为这些端点生成url?url_for('admin.All_RDPs.testindex')url_for('admin.All_RDPs')不起作用。在

Tags: testselflocalhosthttpurlforreturnadmin
1条回答
网友
1楼 · 发布于 2024-06-28 20:07:50

You can override the endpoint name by passing endpoint parameter to the view class constructor:

admin = Admin(app)
admin.add_view(MyView(endpoint='testadmin'))

In this case, you can generate links by concatenating the view method name with an endpoint:

url_for('testadmin.index')

If you don't override the endpoint name, the lower-case class name can be used for generating URLs, like in:

url_for('myview.index')

For model-based views the rules differ - the model class name should be used if an endpoint name is not provided. The ModelView also has these endpoints by default: .index_view, .create_view, and .edit_view. So, the following urls can be generated for a model named "User":

# List View
url_for('user.index_view')

# Create View (redirect back to index_view)
url_for('user.create_view', url=url_for('user.index_view'))

# Edit View for record #1 (redirect back to index_view)
url_for('user.edit_view', id=1, url=url_for('user.index_view'))

来源:Flask-Admin quickstart

相关问题 更多 >