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)