如何在带有中间页面的自定义管理操作后显示确认消息(在 Django 中)?
Django 管理附带的内置操作通常在顶部执行后显示一条有用的消息,例如,添加了一个新对象或您有什么。
文档展示了如何使用可以表示为自定义 ModelAdmin 方法的简单操作来完成此操作。但是,对于需要中间页面的自定义操作(在同一页面上进一步介绍),我鼓励将用户传递到另一个视图。这很好,但这意味着我无法再访问自定义 ModelAdmin 实例来调用其 message_user()
方法...或者至少我不确定如何获取它。
您能否告诉我如何获取当前的 ModelAdmin 实例,或者,如果有更好的方法,当我在另一个视图中完成操作时,如何显示其中一条有用的小消息?
The built-in actions that come with the Django admin generally display a helpful message after they execute at the top, e.g. saying that a new object was added or what have you.
The docs show how to do it with simple actions that can be represented as methods of the custom ModelAdmin. However, with custom actions that need intermediate pages (covered further down on that same page), I am encouraged to pass the user onto another view. That's great, but it means that I no longer have access to the custom ModelAdmin instance in order to call its message_user()
method... Or at least I'm not sure how to get it.
Can you tell me how to get a hold of the current ModelAdmin instance or, if there's a better way, how else to display one of those helpful little messages when I'm done in the other view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要模仿 ModelAdmin.message_user 方法,您只需执行以下操作:
添加消息记录在此处 https://docs.djangoproject.com/en/dev/ref/contrib/messages/#adding-a-message 以及 ModelAdmin 使用它的方式可以在这里看到: https://code. djangoproject.com/browser/django/trunk/django/contrib/admin/options.py#L691
To mimic the ModelAdmin.message_user method you only need to do the following:
Adding a message is documented here https://docs.djangoproject.com/en/dev/ref/contrib/messages/#adding-a-message and the way ModelAdmin uses it can be seen here: https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/options.py#L691
构造一个 LogEntry 并编写一个自定义模板标签以在中间页面上呈现消息,例如:
了解更多: Django 文档(消息框架)
Construct a LogEntry and write a custom templatetag to render messages on your intermediat page, for instance:
read more: Django docs (Message Framework)