get the following error when adding a go back link in my comment_delete.html:
Reverse for 'post_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P[-a-zA-Z0-9_] )/\Z']
Everything works without the go back link (deleting a comment).
Would like to know what I'm doing wrong in href="{% url 'post_detail' post.slug %}"
models.py:
class Post(models.Model):
title = models.CharField(
max_length=200, unique=True,
default=str(f'Fishing {datetime.today()}')[:24]
)
slug = models.SlugField(max_length=200, unique=True)
author = models.ForeignKey(
User, on_delete=models.CASCADE, related_name='blog_posts'
)
featured_image = CloudinaryField('image', default='placeholder')
excerpt = models.TextField(blank=True)
updated_on = models.DateTimeField(auto_now=True)
content = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=STATUS, default=1)
likes = models.ManyToManyField(
User, related_name='blogpost_like', blank=True)
class Meta:
ordering = ['-created_on']
def __str__(self):
return self.title
def number_of_likes(self):
return self.likes.count()
def get_absolute_url(self):
return reverse('post_detail', kwargs={'slug': self.slug})
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
return super().save(*args, **kwargs)
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE,
related_name='comments')
author = models.ForeignKey(User, on_delete=models.CASCADE,
related_name='blog_comments')
body = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
approved = models.BooleanField(default=True)
class Meta:
ordering = ['created_on']
def __str__(self):
return f'Comment {self.body} by {self.author}'
views.py
class CommentDelete(LoginRequiredMixin,
SuccessMessageMixin,
UserPassesTestMixin,
generic.DeleteView):
model = Comment
template_name = 'comment_delete.html'
success_message = "Comment Deleted"
def get_success_url(self):
slug = self.kwargs['slug']
return reverse_lazy('post_detail', kwargs={'slug': slug})
def delete(self, request, *args, **kwargs):
messages.success(self.request, self.success_message)
return super(CommentDelete, self).delete(request, *args, **kwargs)
def test_func(self):
comment = self.get_object()
if self.request.user == comment.author:
return True
return False
urls.py
path('<slug:slug>/comment-delete/<int:pk>', views.CommentDelete.as_view(),
name='comment_delete'),
post_detail.html
{% if request.user == comment.author %}
<a href="{% url 'comment_delete' post.slug comment.pk %}">Delete Comment</a>
{% endif %}
comment_delete.html
<div >
<button type="submit">Delete</button>
<a href="{% url 'post_detail' post.slug %}">Go back</a>
</div>
CodePudding user response:
You are trying to pass slug
of Post
object that is not existing: {% url 'post_detail' post.slug %}
. You need to make sure, that you don't have links to Post
that has been already deleted.
CodePudding user response:
Not sure I understand, I do not what to delete the comment in the post so I want to return to the post instead. If I only do {% url 'post_detail' comment.pk %} I can access delete_comment.html but the link to go back will not work...