0

my superb Internet friends. I have been creating an easy pfc calculator Django app as a beginner. While making it, I have got an error, which is "Generic detail view CommentUpdate must be called with either an object pk or a slug in the URLconf." I don't even know why this error showed up coz I think my way in urls.py is correct.

If you know how to solve this error, please leave your comment below. Any comments help me and thanks for your time in advance!!


# urls.py
  # path('blog/<int:pk>/comment/', views.add_comment, name='add_comment'),

  path('blog/<int:blog_pk>/comment/update/<int:comment_pk>/', CommentUpdate.as_view(), name='comment_update'),
  path('blog/<int:blog_pk>/comment/delete/<int:comment_pk>/', CommentDelete.as_view(), name='comment_delete'),

            <div class="comment-right">
              {% if request.user == blog.user %}
                <a href="{% url 'comment_update' blog.id comment.id %}" class="far fa-edit"></a>
                <a href="{% url 'comment_delete' blog.id comment.id %}" class="far fa-trash-alt"></a>
              {% endif %}
            </div>
# view.py

class CommentUpdate(LoginRequiredMixin, UpdateView):
  model = Comment
  template_name = 'blog/comment_update.html'
  fields = ['text',]
  success_url = reverse_lazy('latest_blogs')
  def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['comment'] = Comment.objects.all().fliter(id=self.request.id)
    return context

#models.py

class Comment(models.Model):
    blog = models.ForeignKey(Blog, on_delete=models.CASCADE, related_name='comments')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='comments', max_length=200)
    text = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    # approved_comment = models.BooleanField(default=False)

    # def approve(self):
    #     self.approved_comment = True
    #     self.save()

    def __str__(self):
        return self.text
Sana
  • 317
  • 3
  • 17

1 Answers1

1

You are only accepting one argument in your view function. Make two parameters and try to print them, and see what argument you are getting in which variable.

Akshay
  • 71
  • 3
  • so sorry that I put wrong views, so I changed them. And, I have been working on it , I still have an error "Generic detail view CommentUpdate must be called with either an object pk or a slug in the URLconf." I have no idea about this. – Sana Jun 26 '21 at 12:52
  • Founded relevant question https://stackoverflow.com/questions/11494483/django-class-based-view-how-do-i-pass-additional-parameters-to-the-as-view-meth – Akshay Jun 27 '21 at 09:17