我建立了一个通知系统。当"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 %}我想要添加的是没有模板的通知删除功能(删除视图)。
当用户单击通知并重定向到url时,我希望删除该通知。
我有办法这么做吗?
发布于 2020-04-24 23:07:15
您的代码有点混乱,但我认为最好使用DRF并删除通知,而不像@Edgardo_Obregón提到的那样重定向。或者,如果您不想使用DRF,那么如果您只使用一个小视图是个好主意:
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt # if you wanna bypass csrf, otherwise you can use csrf with ajax docs
def delete_notification(request,pk):
if request.METHOD == "DELETE": # optional
notification = Notification.objects.get(pk=pk)
if notification is not None:
notification.delete()https://stackoverflow.com/questions/61418072
复制相似问题