# How to show django messages based on queryset filter

**URL:** <https://forum.djangoproject.com/t/how-to-show-django-messages-based-on-queryset-filter/8276>\
**Category:** Using Django\
**Created:** [June 9, 2021, 12:28pm UTC](https://forum.djangoproject.com/t/how-to-show-django-messages-based-on-queryset-filter/8276 "2021-06-09T12:28:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MDFARHYN](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mdfarhyn/32/4519_2.png) [@MDFARHYN](https://forum.djangoproject.com/u/MDFARHYN)\
**Post date:** [June 9, 2021, 12:28pm UTC](https://forum.djangoproject.com/t/how-to-show-django-messages-based-on-queryset-filter/8276/1 "2021-06-09T12:28:07Z")

</div>

I want to show message based on my filter.

```auto
CHOICES = (

        ('0', 'published',),

        ('1', 'pending',),

        ('2', 'rejected',),

        )

```

Here I tried this code for showing message based on filter but it’s only showing the published message.

```auto
def get_context_data(self, **kwargs):

            data = super().get_context_data(**kwargs)

            published = BlogComment.objects.filter(is_published="0")

            pending = BlogComment.objects.filter(is_published="1")

            

            if published:

                 messages.add_message(self.request, messages.INFO, 'Comment status published Sucessfully')

            elif pending: 

               messages.add_message(self.request, messages.INFO, 'Comment status pending Sucessfully')

            

            return data

```

why I am only getting the published message?

I also tried if statement instead of elif. After using if statement I am getting three message at time while changing status of any object.

```auto
            if published:
                 messages.add_message(self.request, messages.INFO, 'Comment Published Sucessfully')
            if pending: 
                 messages.add_message(self.request, messages.INFO, 'Comment Status Pending')
            if rejected:
                 messages.add_message(self.request, messages.INFO, 'Comment Status Rejected')

```

I aslo tried to use pk in filter but didn’t work.  
`BlogComment.objects.filter(is_published="0", pk=True)`

```auto

```

---

<div class="post-metadata">

**Author:** ![MDFARHYN](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mdfarhyn/32/4519_2.png) [@MDFARHYN](https://forum.djangoproject.com/u/MDFARHYN)\
**Post date:** [June 10, 2021, 12:57pm UTC](https://forum.djangoproject.com/t/how-to-show-django-messages-based-on-queryset-filter/8276/2 "2021-06-10T12:57:12Z")

</div>

I find out the solution. here I am sharing the code. hope it will help others.

```auto
def form_valid(self, form):

        self.object = form.save()

        status = self.object.is_published

        name = self.object.name[0:20]

        messages.success(self.request, f"{name} comment status {status}")

        return super().form_valid(form)

```
