我正在用博客和博客评论构建一个简单的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"/>
答案 0 :(得分:2)
您运行python manage.py makemigrations
和python manage.py migrate
吗?
如果它是一个新应用,您是否将其添加到settings.py?