Django 中的评论线程



我正在构建一个简单的Django应用程序,其中包含博客和博客评论。在开发评论线程时,当我尝试输入评论时,我收到错误"没有这样的表:blog_commentsonpost"。有人可以帮助我解决问题吗?

models.py

class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
title = models.CharField(max_length=100)
description = models.CharField(max_length=1000)
def publish(self):
self.published_date = timezone.now()
self.save()
def get_type(self):
return "post"
def get_absolute_url(self):
return reverse("post_detail", kwargs={'pk': self.pk})
def __str__(self):
return self.title
class CommentsOnPost(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='comment')
post = models.ForeignKey('blog.Post', on_delete=models.CASCADE, related_name='comments')
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
text = models.CharField(max_length=1000)
@classmethod
def create(cls, author, text, post):
postcomment = cls(author=author,
post_id=post,
text=text, created_date=timezone.now,
published_date=timezone.now)
return postcomment

views.py

class PostDetailView(DetailView):
model = Post
redirect_field_name = 'post_detail'
def post(self, request, *args, **kwargs):
author = request.user
text  = request.POST['postcomment']
post  = request.POST['post']
postcomment = CommentsOnPost()
postcomment.text = text
postcomment.author = author
postcomment.save()
return HttpResponseRedirect('/blog/post/{}'.format(post))

.html

<form method="POST" class="post-form" >{% csrf_token %}
<div class="form-group">
<label for="postcomment">Comments:</label>
<textarea class="form-control" name="postcomment" rows="5"></textarea>
</div>
<input type="hidden" name="post" value="{{ post.id }}">
<div>
<input type="submit" class="btn btn-primary" style="color:blue;" value="Comment"/>

你跑python manage.py makemigrationspython manage.py migrate吗?

如果是新应用程序,您是否将其添加到 settings.py?

最新更新