遇到Django中的通知功能问题。

huangapple go评论88阅读模式
英文:

Having trouble with notifications feature in Django

问题

  1. <div class="dropdown-content" id="myDropdown">
  2. {% if notifications %}
  3. {% for notification in notifications %}
  4. {% if notification.notification_type == 'created' %}
  5. <a href="#">{{notification.ticket.name}}: 有新工单分配给你。</a>
  6. {% else %}
  7. <a href="#">{{notification.ticket.name}}: 工单已更新。</a>
  8. {% endif %}
  9. {% endfor %}
  10. {% else %}
  11. <a href="#">没有新通知。</a>
  12. {% endif %}
  13. </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. All it currently displays is 'The ticket has been updated' instead of 'A new ticket has been assigned to you.' How do I fix this?

Here is what i have so far.

template

  1. &lt;div class=&quot;dropdown-content&quot; id=&quot;myDropdown&quot;&gt;
  2. {% if notifications %}
  3. {% for notification in notifications %}
  4. {% if notification.notification_type == &#39;created&#39; %}
  5. &lt;a href=&quot;#&quot;&gt;{{notification.ticket.name}}: A new ticket has been assigned to you.&lt;/a&gt;
  6. {% else %}
  7. &lt;a href=&quot;#&quot;&gt;{{notification.ticket.name}}: The ticket has been updated.&lt;/a&gt;
  8. {% endif %}
  9. {% endfor %}
  10. {% else %}
  11. &lt;a href=&quot;#&quot;&gt;No new notifications&lt;/a&gt;
  12. {% endif %}
  13. &lt;/div&gt;

models.py for notification

  1. class Notification(models.Model):
  2. OPTIONS = (
  3. (&#39;created&#39;, &#39;created&#39;),
  4. (&#39;updated&#39;, &#39;updated&#39;),
  5. )
  6. recipient = models.ForeignKey(CustomUser, on_delete=models.SET_NULL, null=True, related_name=&#39;notifications&#39;)
  7. ticket = models.ForeignKey(Ticket, on_delete=models.SET_NULL, null=True, related_name=&#39;notifications&#39;)
  8. message = models.TextField()
  9. notification_type = models.CharField(choices=OPTIONS, max_length=15, null=True)
  10. is_read = models.BooleanField(default=False)
  11. created = models.DateTimeField(auto_now_add=True)

signals.py for notification

  1. @receiver(post_save, sender=Ticket)
  2. def create_notification(sender, instance, created, **kwargs):
  3. if created:
  4. notification_type = &#39;created&#39;
  5. else:
  6. notification_type = &#39;updated&#39;
  7. assignees = instance.assignee.all()
  8. for assignee in assignees:
  9. Notification.objects.create(recipient=assignee, notification_type=notification_type)

this is createTicket function inside the views.py

  1. @login_required
  2. def createTicket(request):
  3. form = TicketForm()
  4. categories = Category.objects.all()
  5. users = CustomUser.objects.all()
  6. if request.method == &#39;POST&#39;:
  7. category_name = request.POST.get(&#39;category&#39;)
  8. category, created = Category.objects.get_or_create(name=category_name)
  9. # Retrieve the project object that the ticket should be connected with
  10. project_id = request.POST.get(&#39;project&#39;)
  11. project = Project.objects.get(id=project_id)
  12. # Retrieve the list of selected users from the form
  13. assignee_ids = request.POST.getlist(&#39;assignee&#39;)
  14. assignee = CustomUser.objects.filter(id__in=assignee_ids)
  15. ticket = Ticket.objects.create(
  16. host = request.user,
  17. category=category,
  18. project=project,
  19. name=request.POST.get(&#39;name&#39;),
  20. status=request.POST.get(&#39;status&#39;),
  21. priority=request.POST.get(&#39;priority&#39;),
  22. type=request.POST.get(&#39;type&#39;),
  23. description=request.POST.get(&#39;description&#39;),
  24. )
  25. # Add the selected users to the ticket
  26. ticket.assignee.set(assignee)
  27. ticket.save()
  28. return redirect(&#39;ticket&#39;, pk=ticket.id)
  29. context = {&#39;form&#39;: form, &#39;categories&#39;: categories, &#39;users&#39;: users}
  30. return render(request, &#39;tickets/ticket_form.html&#39;, context)

答案1

得分: 0

  1. ticket = Ticket.objects.create(
  2. host=request.user,
  3. category=category,
  4. project=project,
  5. name=request.POST.get('name'),
  6. status=request.POST.get('status'),
  7. priority=request.POST.get('priority'),
  8. type=request.POST.get('type'),
  9. description=request.POST.get('description'),
  10. )
  11. # 将选择的用户分配给工单
  12. ticket.assignee.set(assignee)
  13. ticket.save()

这将触发两次信号,一次是create,另一次是save

你不需要最后一行,只需要写:

  1. ticket.assignee.set(assignee)

就足够了。

另外,当你创建工单并且信号触发时,你还没有assignee,你应该通过m2m_changed信号来实际处理它。

  1. <details>
  2. <summary>英文:</summary>

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'),
)

  1. # Add the selected users to the ticket
  2. ticket.assignee.set(assignee)
  3. ticket.save()
  1. This will trigger your signals twice, 1 for `create` and another one for `save`.
  2. You don&#39;t need the last line, just put

ticket.assignee.set(assignee)

  1. is enough.
  2. Btw when you create a ticket and signal triggers, you don&#39;t have any `assignee` yet, you should actually do it via [m2m_changed][1] signal
  3. [1]: https://docs.djangoproject.com/en/4.1/ref/signals/#m2m-changed
  4. </details>

huangapple
  • 本文由 发表于 2023年2月6日 08:52:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定