首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Django中,如何通过单击链接来删除模板中的对象?

在Django中,如何通过单击链接来删除模板中的对象?
EN

Stack Overflow用户
提问于 2020-04-24 21:55:57
回答 1查看 33关注 0票数 0

我建立了一个通知系统。当"B“用户评论" A”用户的帖子时,通知发送给A。

这是我代码的一部分。

代码语言:javascript
复制
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)
代码语言:javascript
复制
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"
代码语言:javascript
复制
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})
代码语言:javascript
复制
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时,我希望删除该通知。

我有办法这么做吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-24 23:07:15

您的代码有点混乱,但我认为最好使用DRF并删除通知,而不像@Edgardo_Obregón提到的那样重定向。或者,如果您不想使用DRF,那么如果您只使用一个小视图是个好主意:

代码语言:javascript
复制
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()

带有ajax的csrf文档

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61418072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档