/post/3/comment/ 'dict' 对象的类型错误不可调用

class Comment(models.Model):

    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')

    author = models.CharField(max_length=200)

    text = models.TextField()

    image = models.ImageField(upload_to='Comment_Pictures', blank=True)

    date_posted = models.DateTimeField(default=timezone.now)


    def __str__(self):

        return self.text


    def get_absolute_url(self):

        return '/'



class CommentAddView(LoginRequiredMixin, CreateView):

    model = Comment

    fields = ['text', 'image']


    def form_valid(self, form):

        form.instance.author = self.request.user

        form.instance.post = Post.objects.get(id=self.kwargs['pk'])

        return super().form_valid(form)


class CommentDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):

    model = Comment

    success_url = '/'


    def test_func(self):

        comment = self.get_object()

        if request.user == comment.author:

            return True

        return False

TypeError at /post/3/comment/ 'dict' 对象不可调用请求方法:POST 请求 URL: http://localhost:8000/post/3/comment/ Django 版本:2.1.7 异常类型:TypeError 异常值: “dict”对象不可调用


猛跑小猪
浏览 237回答 2
2回答

万千封印

错误告诉您究竟出了什么问题。self.kwargs是一个字典,但你正在调用它。你可能的意思是:data = Post.objects.get(id=self.kwargs['pk'])

GCT1015

你也可以试试这个:form.instance.post = Post.objects.get(id=self.kwargs.get('pk'))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python