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”对象不可调用
万千封印
GCT1015
相关分类