0

TOday I am trying to make some kind of to-do list. I know how to add tasks(comments) and now I want to delete them with a button. I don't know how to delete exact task (comment). Code:

#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>

So how to make "magic" happen?

Addition

Now I'm facing new problems. Delete button does nothing or i get eroor: invalid literal for int() with base 10: ''. Code:

#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,
        })

Can you help me solve this one?

2
  • In my opinion, it is not a good idea to have a delete functionality in the add method. Commented Mar 22, 2014 at 21:25
  • I got this error: Comment object can't be deleted because its id attribute is set to None. Commented Mar 24, 2014 at 6:07

1 Answer 1

2

My solution will be to add a function delete in your views that will take for argument the comment number.

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

and your url:

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

in your template link your comment button delete to this url

yoururl/del/{{yourvalue of the comment that will give the id of the current comment}}

example in templates:

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

There is another solution that may work:

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

the hidden field should look like this in your template:

<input type="hidden" name="idcomment" id="{{comments.id}}" /> #or value="{{comments.id}} sorry i do not have my own example on hand.
Sign up to request clarification or add additional context in comments.

5 Comments

I might be wrong, but with this code I will have comments on seperate pages, am I right? While, I want to have all my comments in one page as a list.
I don't think so if you list your comment with a boucle for and add the id of each comment so when someone press the button for that particular comment it will delete this one only.
Thanks for helping, but if I want to have and adding form and deleting on the one page. How I need to change the url to make your desicion work out?
The first solution only will create a link that once clicked on it will call the views function to delete the comment. you can add return HttpResponseRedirect('the url you want to redirect') and you will be back to the comment one, if you want to stay in the same page I think you need to take a look to ajax and I won't be able to help you on that.
replace id="{{comments.id}}" with id="{{a.id}}" since it is in the boucle For

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.