Я изучаю Django и работаю над системой комментариев.
Модель
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
comment_user = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
active = models.BooleanField(default=True)
class Meta:
ordering = ('created',)
def __str__(self):
return 'comment by {} on {}'.format(self.comment_user, self.post)
Просмотры
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug=post, status='published', publish__year=year, publish__month=month,
publish__day=day)
comments = post.comments.filter(active=True)
new_comment = None
if request.method == 'POST':
new_comment = CommentForm(request.POST)
if CommentForm.is_valid:
new_comment = CommentForm.save(commit=False)
new_comment.post = post
new_comment.comment_user = request.user
new_comment.save()
else:
new_comment = CommentForm
context = {
'post': post,
'comments':comments,
'new_comment': new_comment,
'CommentForm': CommentForm
}
return render(request, 'blog/post/detail.html', context)
Когда я пытаюсь создать новый комментарий, этовозникает ошибка
TypeError at /blog/2019/10/29/my-second-post/
save() missing 1 required positional argument: 'self'
Request Method: POST
Request URL: http://127.0.0.1:8000/blog/2019/10/29/my-second-post/
Django Version: 2.2.6
Exception Type: TypeError
Exception Value:
save() missing 1 required positional argument: 'self'
Exception Location: E:\v_envs\elkhashen\src\blog\views.py in post_detail, line 35
Python Executable: E:\v_envs\elkhashen\Scripts\python.exe
Python Version: 3.7.4
Python Path:
['E:\\v_envs\\elkhashen\\src',
'E:\\v_envs\\elkhashen\\Scripts\\python37.zip',
'E:\\v_envs\\elkhashen\\DLLs',
'E:\\v_envs\\elkhashen\\lib',
'E:\\v_envs\\elkhashen\\Scripts',
'c:\\users\\elkhashen\\appdata\\local\\programs\\python\\python37-32\\Lib',
'c:\\users\\elkhashen\\appdata\\local\\programs\\python\\python37-32\\DLLs',
'E:\\v_envs\\elkhashen',
'E:\\v_envs\\elkhashen\\lib\\site-packages']
Server time: Thu, 31 Oct 2019 09:50:45 +0000