Я сделал систему уведомлений. Когда пользователь "B" комментирует сообщение пользователя "A", уведомление отправляется A.
Это моя часть моего кода.
models.py
from django.db import models
from freeboard.models import FreeBoardComment
from users.models import CustomUser
class Notification(models.Model):
TYPE_CHOCIES = (
("FreeBoardComment", "FreeBoardComment"),
)
creator = models.ForeignKey(CustomUser, on_delete=models.CASCADE, null=True, related_name="creator")
to = models.ForeignKey(CustomUser, on_delete=models.CASCADE, null=True, related_name="to")
notification_type = models.CharField(max_length=50, choices=TYPE_CHOCIES)
comment = models.CharField(max_length=1000, blank=True, null=True)
post_id = models.IntegerField(null=True)
class Meta:
ordering = ["-pk"]
def __str__(self):
return "From: {} - To: {}".format(self.creator, self.to)
notification/views.py
from django.views.generic import ListView
from .models import Notification
def create_notification(creator, to, notification_type, comment, post_id):
if creator.email != to.email:
notification = Notification.objects.create(
creator=creator,
to=to,
notification_type=notification_type,
comment=comment,
post_id=post_id,
)
notification.save()
class NotificationView(ListView):
model = Notification
template_name = "notification/notification.html"
freeboard/views.py
...
@login_required
def comment_write(request, pk):
post = get_object_or_404(FreeBoardPost, pk=pk)
if request.method == 'POST':
form = CreateFreeBoardComment(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.comment_writer = request.user
comment.post = post
comment.save()
# 포인트
award_points(request.user, 1)
### NOTIFICATION PART!!!! ###
create_notification(request.user, post.author, "FreeBoardComment", comment.comment_text, post.pk)
### NOTIFICATION PART !!! ###
return redirect("freeboard_detail", pk=post.id)
else:
form = CreateFreeBoardComment()
return render(request, "bbs/freeboard/free_board_comment.html", {"form": form})
notification.html
{% extends "base.html" %}
{% block css_file %}
<link href="/static/css/notification/notification.css" rel="stylesheet">
{% endblock %}
{% block content %}
<ul class="list-group">
{% for notification in notification_list %}
<li class="list-group-item dropdown">
<a href="{% if notification.notification_type == "FreeBoardComment" %}/free/{{ notification.post_id }}{% endif %}"
class="dropdown-toggle" style="color: #555; text-decoration: none;">
<div class="media">
<img src="{{ notification.creator.image.url }}" width="50" height="50"
class="pull-left img-rounded" style="margin: 2px"/>
<div class="media-body">
<h4 class="media-heading"><span
class="genre">[@{{ notification.to.nickname }}]</span> {{ notification.comment }}
</h4>
<span style="font-size: 0.9rem;"><i
class="fa fa-user"></i> {{ notification.creator.nickname }}</span>
</div>
</div>
</a>
</li>
{% endfor %}
</ul>
{% endblock %}
И я пытаюсь добавить функцию удаления уведомлений БЕЗ ШАБЛОНА (deleteview).
Когда пользователь щелкает уведомление и перенаправляет его на URL, я хочу удалить это уведомление.
Есть ли способ Я могу это сделать?