0

I'm trying to allow users to delete their own comments. I received this error: ValueError: invalid literal for int() with base 10: 'testuser1-2 I also got this error: MultipleObjectsReturned get() returned more than one Comment -- it returned 4!

Any idea whats wrong with my code? let if know if you need any other info

html

  {% for comment in blog_post.comments.all %}
  
  <br>
  {{ comment.body }}
  {% if comment.name == request.user %}

  <form action = "{% url 'HomeFeed:deletecomments' comment.post_id %}" method = "POST">     {% csrf_token %}

  <button>Delete</button>
</form>
     {% endif %}

  
  {% endfor %}

urls.py

    path('comments/<slug>', AddCommentView.as_view(), name= "add_comment"),
    path('deletecomments/<post_id>', delete_own_comment, name= "deletecomments"),

views.py

@login_required
def delete_own_comment(request, slug):
    if request.method == 'POST':
        post = BlogPost.objects.get(slug=slug)
        comment = get_object_or_404(Comment, post=post)
        if comment.name == request.user:
            comment.delete()
        return redirect('HomeFeed:detail', slug=slug)

models.py

class Comment(models.Model):
   post = models.ForeignKey(BlogPost, related_name='comments', on_delete=models.CASCADE)
   name = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='name', on_delete=models.CASCADE)
   body = models.TextField()
   date_added = models.DateField(auto_now_add=True)

class BlogPost(models.Model):
 chief_title                    = models.CharField(max_length=50, null=False, blank=False, unique=True)
 brief_description = models.TextField(max_length=300, null=False, blank=False)
 author                     = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
 slug                   = models.SlugField(blank=True, unique=True)

  • `get` should always return a record and only one record, else you will get an error. Your query is returning 4 records, better use `filter`. – Ejaz Jan 24 '21 at 05:42

1 Answers1

0

Try this:

@login_required
def delete_own_comment(request, post_id):
    if request.method == 'POST':
        post = BlogPost.objects.get(id=post_id)
        # pay attention i use `post` instead of `post_id`
        comment = get_object_or_404(Comment, post=post)
        if comment.name == request.user:
            comment.delete()
        return redirect('HomeFeed:detail', slug=slug)

and also update your urls.py by adding int like that:

path('deletecomments/<int:post_id>', delete_own_comment, name= "deletecomments"),

This will make sure that your post_id is converted into int before passing it to your view.By the way its optional ;)

Mubashar Javed
  • 1,197
  • 1
  • 9
  • 17
  • Hey bro! Thanks for the help, I’ll try it out later. Just wondering also, if I want to give the blog post author the permission to delete any comment that’s on their post, can I just create a any function and use: comment.blogpost.user = request.user –  Jan 24 '21 at 07:24
  • Lets have a look at a [stackoverflow](https://stackoverflow.com/questions/49003144/django-python-adding-custom-permissions-to-specific-users) answer. For custom permission to a specific user. – Mubashar Javed Jan 24 '21 at 08:37
  • And [django's docs](https://docs.djangoproject.com/en/3.1/topics/auth/customizing/#custom-users-and-permissions) link too. – Mubashar Javed Jan 24 '21 at 08:38
  • Hi bro, if you have time, could you check out this question @Mubasharjaved https://stackoverflow.com/questions/65870615/valueerror-invalid-literal-for-int-with-base-10-slug-1-2 –  Jan 24 '21 at 14:07