1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Improved message example in admin actions documentation.

Avoid partial string construction and make use of ``ngettext`` to show
example of how to handle plural variants with translations. Also make
use of ``messages.SUCCESS`` to highlight customizing the style of the
message - in this case it better fits what the message is conveying.
This commit is contained in:
Nick Pope 2020-04-16 10:26:33 +01:00 committed by GitHub
parent daabb102c0
commit 058b38b43e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -189,16 +189,19 @@ provided by the admin.
For example, we can use ``self`` to flash a message to the user informing her For example, we can use ``self`` to flash a message to the user informing her
that the action was successful:: that the action was successful::
from django.contrib import messages
from django.utils.translation import ngettext
class ArticleAdmin(admin.ModelAdmin): class ArticleAdmin(admin.ModelAdmin):
... ...
def make_published(self, request, queryset): def make_published(self, request, queryset):
rows_updated = queryset.update(status='p') updated = queryset.update(status='p')
if rows_updated == 1: self.message_user(request, ngettext(
message_bit = "1 story was" '%d story was successfully marked as published.',
else: '%d stories were successfully marked as published.',
message_bit = "%s stories were" % rows_updated updated,
self.message_user(request, "%s successfully marked as published." % message_bit) ) % updated, messages.SUCCESS)
This make the action match what the admin itself does after successfully This make the action match what the admin itself does after successfully
performing an action: performing an action: