# Having trouble with notifications feature in Django

**URL:** <https://forum.djangoproject.com/t/having-trouble-with-notifications-feature-in-django/18660>\
**Category:** Mystery Errors\
**Created:** [February 6, 2023, 1:14am UTC](https://forum.djangoproject.com/t/having-trouble-with-notifications-feature-in-django/18660 "2023-02-06T01:14:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mdaw11](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Mdaw11](https://forum.djangoproject.com/u/Mdaw11)\
**Post date:** [February 6, 2023, 1:14am UTC](https://forum.djangoproject.com/t/having-trouble-with-notifications-feature-in-django/18660/1 "2023-02-06T01:14:50Z")

</div>

Created a notification feature in my django project. I’ve got all the functionality down and it works except for one thing. I want the template to display a different message depending on what action is committed. It only displays ‘The ticket has been updated’ no matter what action is taken instead of displaying the correct message How do I fix this?

Here is what i have so far.

template

```auto
<div class="dropdown-content" id="myDropdown">
          {% if notifications %}
            {% for notification in notifications %}
              {% if notification.notification_type == 'created' %}
                <a href="#">{{notification.ticket.name}}: A new ticket has been assigned to you.</a>
              {% else %}
                <a href="#">{{notification.ticket.name}}: The ticket has been updated.</a>
              {% endif %}
            {% endfor %}
          {% else %}
            <a href="#">No new notifications</a>
          {% endif %}
</div>

```

models.py for notification

```auto
class Notification(models.Model):
    OPTIONS = (
        ('created', 'created'),
        ('updated', 'updated'),
    )
    
    recipient = models.ForeignKey(CustomUser, on_delete=models.SET_NULL, null=True, related_name='notifications')
    ticket = models.ForeignKey(Ticket, on_delete=models.SET_NULL, null=True, related_name='notifications')
    message = models.TextField()
    notification_type = models.CharField(choices=OPTIONS, max_length=15, null=True)
    is_read = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)

```

signals.py for notification

```auto
@receiver(post_save, sender=Ticket)
def create_notification(sender, instance, created, **kwargs):
    if created:
        notification_type = 'created'
    else:
        notification_type = 'updated'
        
    assignees = instance.assignee.all()
    for assignee in assignees:
        Notification.objects.create(recipient=assignee, notification_type=notification_type)

```

this is createTicket function inside the views.py

```auto
@login_required
def createTicket(request):
    form = TicketForm()
    
    categories = Category.objects.all()
    users = CustomUser.objects.all()
    if request.method == 'POST':
        category_name = request.POST.get('category')
        category, created = Category.objects.get_or_create(name=category_name)
        
        # Retrieve the project object that the ticket should be connected with
        project_id = request.POST.get('project')
        project = Project.objects.get(id=project_id)
        
        # Retrieve the list of selected users from the form
        assignee_ids = request.POST.getlist('assignee')
        assignee = CustomUser.objects.filter(id__in=assignee_ids)
        
        ticket = Ticket.objects.create(
            host = request.user,
            category=category,
            project=project,
            name=request.POST.get('name'),
            status=request.POST.get('status'),
            priority=request.POST.get('priority'),
            type=request.POST.get('type'),
            description=request.POST.get('description'),
        )
        
        # Add the selected users to the ticket
        ticket.assignee.set(assignee)
        ticket.save()
        
        return redirect('ticket', pk=ticket.id)
    
    context = {'form': form, 'categories': categories, 'users': users}
    return render(request, 'tickets/ticket_form.html', context)

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 6, 2023, 2:18am UTC](https://forum.djangoproject.com/t/having-trouble-with-notifications-feature-in-django/18660/2 "2023-02-06T02:18:56Z")

</div>

You’ve got two statements here that are saving data to the database:

> [@Mdaw11](#):
>
> `ticket = Ticket.objects.create(`

and

> [@Mdaw11](#):
>
> `ticket.save()`

It doesn’t matter what’s happening with the `.create`, but the `ticket.save()` is going to be updating an existing instance.

What I’m not sure of is why you’re only getting one message and not two. It would be interesting to check the database after that view is executed to see what’s in that table.

(I’m guessing that there’s a lot more to your “Notification” system than what’s shown here, so the full explanation is probably in some of the code you’re not showing.)
