How do I return the url back to details page once the comment is deleted – Django?
Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error How do I return the url back to details page once the comment is deleted – Django? on this date .
So at the moment once the comment is deleted, the URL takes back to the post list page.
As probably you could tell that I am not very experienced with Django yet so if someone could explain that how do I figure out what key is to be passed and where, that would be a great help.
Please find the codes below:
MODELS: class Post(models.Model): CHOICES = ( ('celebrate', 'celebrate'), ('planning', 'planning'), ('outdoor', 'outdoor'), ('holidays', 'holidays'), ('festivals', 'festivals'), ('movies', 'movies'), ('shopping', 'shopping'), ('laptop', 'laptop'), ('data', 'data'), ('sciance', 'science'), ('summers', 'summers'), ('medical', 'medical'), ('art', 'art'), ) author = models.ForeignKey(User, on_delete=models.CASCADE, default="") title = models.CharField(max_length=200) date_posted = models.DateTimeField(default=timezone.now) text = models.TextField() category = models.CharField(max_length=100, null=True, choices=CHOICES) tags = models.ManyToManyField(Tag) class Meta: """ Meta class to change the configuration, ordering by the name""" ordering = ['-date_posted'] def get_absolute_url(self): """ Reverse the Post object to the url once action has been taken with primary key to direct back to the same post """ return reverse('posts:post_list') def __str__(self): return self.title class Comment(models.Model): """ comments for Post model """ comment_post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comment_for_post', null=True, default='') comment_text = models.TextField(null=True) date_posted = models.DateTimeField(auto_now=True) def get_absolute_url(self): """ Reverse the Post object to the url once action has been taken with primary key to direct back to the same post """ return reverse('posts:post_details', kwargs={'pk': self.pk}) def __str__(self): return self.comment_text VIEWS: @login_required def add_comment_to_post(request, pk): """ To attach comments to the post""" post = get_object_or_404(models.Post, pk=pk) if request.method == 'POST': form = forms.CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.comment_post = post comment.save() return redirect('posts:post_details', pk=post.pk) else: form = forms.CommentForm() return render(request, 'posts/comment_form.html', context={'form': form}) class CommentDeleteView(LoginRequiredMixin, generic.DeleteView): model = models.Comment template_name = 'posts/comment_confirm_delete.html' def delete(self, *args, **kwargs): self.object = self.get_object() super().delete(*args, **kwargs) def get_success_url(self): return reverse_lazy('posts:post_list') URLS: urlpatterns = [ path('post_list/', views.PostListView.as_view(), name='post_list'), path('user/<str:username>/', views.UserPostListView.as_view(), name='user_posts'), path('create/', views.PostCreateView.as_view(), name='create'), path('details/<int:pk>/', views.PostDetailView.as_view(), name='post_details'), path('delete/<int:pk>/', views.PostDeleteView.as_view(), name='post_delete'), path('update/<int:pk>/', views.PostUpdateView.as_view(), name='post_update'), path('<str:category>/', views.CategoryPostListView.as_view(), name='category_posts'), path('posts/<int:pk>/comment', views.add_comment_to_post, name='post_comment_list'), path('delete_comment/<int:pk>/', views.CommentDeleteView.as_view(), name='delete_comment'), ] TEMPLATE: <!--COMMNET SECTION --> <div class='container'> {% for comment in post.comment_for_post.all %} <br> {% if object.author == user %} <div class="comment-post"> <ul> <li> <p >{{comment.comment_text|safe}}, <span><a href="{#}"> Reply</a></span></p> <p>From {{user.username}} on {{comment.date_posted|date:'d-M-y'}}, <span><a href="{% url 'posts:delete_comment' comment.id %}">Delete comment?</a></span> </p> </li> </ul> </div> {%else %} <div class="comment-post"> <ul> <li> <p >{{comment.comment_text|safe}}, <span><a href="{#}"> Reply</a></span></p> </p> <p>From {{user.username}} on {{comment.date_posted|date:'d-M-y'}}, </li> </ul> </div>
Answer
def get_success_url(self): return reverse_lazy('posts:post_details', kwargs={'pk': self.object.comment_post.id})