Home > Back-end >  Pluralize Django message
Pluralize Django message

Time:04-11

I have the following code to display a message when a custom action is executed in the Admin site:

messages.info(request, '%s posts marked as Draft' % queryset.count())

What is the best way to pluralize that message for when the count is greater than 1?

1 post marked as Draft
3 posts marked as Draft

CodePudding user response:

Use pluralize (and probably don't use the older style % formatting):

from django.template.defaultfilters import pluralize

post_count = queryset.count()
messages.info(
    request, '{} {} marked as Draft'.format(
        post_count, pluralize(post_count, 'post,posts')))
  • Related