如何从数据库中删除确切的元素?

时间:2014-03-22 20:12:18

标签: django

我正试图制作某种待办事项清单。我知道如何添加任务(评论),现在我想用一个按钮删除它们。我不知道如何删除确切的任务(评论)。代码:

#views.py

def add_comment(request):
    comments = Comment.objects.all()
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if "delete" in request.POST:

            #HERE MAGIC HAPPENS

        if form.is_valid():
            save_it = form.save()
        return render(request, 'task-result.html', {
        'form': form, 'comments': comments,
        })
    else:
        form = CommentForm()
    return render(request, 'Task-form.html', {
        'form': form,
        })

#HTML

<form action="">
      {% for a in comments %}
          <h3>{{ a.body}}</h3>
          <input type="submit" name="delete" value="delete" />
      {% endfor %}
      {% csrf_token %}
</form>

那么如何让“神奇”发生呢?

加成

现在我正面临新问题。删除按钮什么都不做,或者我得到了eroor:对于带有基数10的int()的无效文字:''。代码:

#Template: 

<html>
<head>
    <title>Name</title>
</head>
<body>
    <h1>Tasks</h1>
    <form action="" method="post">
      {{ form.as_p }}
      <input type="submit" value="Create task">
      {% for a in comments %}
      <h3>{{ a.body}}</h3>
      <input type="submit" name="delete" value="delete" />
      <input type="hidden" name="idcomment" id="{{comments.id}}" />
      {% csrf_token %}
    </form>
    {% endfor %}
</body>
</html>

#Views

def add_comment(request):
    comments = Comment.objects.all()
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if "delete" in request.POST:
            comments_id = request.POST['idcomment']
            comments_object = Comment.objects.get(id=comments_id)
            comments_object.delete()
        if form.is_valid():
            save_it = form.save()
        return render(request, 'task-form.html', {
        'form': form, 'comments': comments,
        })
    else:
        form = CommentForm()
    return render(request, 'Task-form.html', {
        'form': form, 'comments': comments,
        })

你能帮我解决这个吗?

1 个答案:

答案 0 :(得分:2)

我的解决方案是在您的视图中添加一个函数delete,用于参数注释编号。

def del_comment(request, commentsid):
    comments = Comment.objects.get(id=commentsid)
    comments.delete()

和你的网址:

url(r'^yoururl/del/(?P<commentsid>\d+)/', del_comment),

在您的模板链接中将您的评论按钮删除到此网址

yoururl/del/{{yourvalue of the comment that will give the id of the current comment}}
模板中的

示例:

  {% for a in comments %}
      <h3>{{ a.body}}</h3>
  <a HREF="/yoururl/del/{{a.id}}"> Delete ME </a>
  {% endfor %}

还有另一种解决方案可行:

   if request.method == 'POST':
        form = CommentForm(request.POST)
        if "delete" in request.POST:
            comments_id = request.POST['idcomment']  #the name of the hidden field and get the id of the comment.
            comments_object = Comment.objects.get(id=comments_id)
            comments_object.delete()                

        if form.is_valid():
            save_it = form.save()
        return render(request, 'task-result.html', {
        'form': form, 'comments': comments,
        })

隐藏字段在模板中应如下所示:

<input type="hidden" name="idcomment" id="{{comments.id}}" /> #or value="{{comments.id}} sorry i do not have my own example on hand.