我试图在我的post_detail中添加评论表:评论表

时间:2020-04-23 09:28:26

标签: django django-models django-forms django-views django-templates

我正在尝试在我的post_detail中添加注释表单,以显示错误(django.db.utils.OperationalError:无此类列:userpost_comments.posts_id),有人可以帮我吗,我正在使用类视图作为我的detailsview

models.py

class Comments(models.Model):
posts = models.ForeignKey(Post,on_delete=models.CASCADE,related_name='comments')
author= models.ForeignKey(User,on_delete=models.CASCADE)
comment = models.TextField()
commented = models.DateTimeField(default=timezone.now)

  def __str__(self):
    return '{}-{}'.format(self.Post.title, str(self.User.username))

forms.py

class Commentform(forms.ModelForm):
  class Meta:
    model = Comments
    fields = ('comment',)

views.py

class PostDetailViews(DetailView):
model = Post

@login_required()
def Commentviews(request, slug):
      post = get_object_or_404(Post, slug=slug)
      template_name ='userpost/post_detail.html'
      if request.method == 'POST':
         form = Commentform(request.POST)
         if form.is_valid():
             comment = form.save(commit=False)
             comment.post = post
             comment.save()
             return render(request,'userpost/post_detail.com',slug=post.slug)
      else:
         form = Commentform
         template = 'userpost/post_detail.html'
         context = {'form':form}
     return render(request, template, context)

post_details.html

<div class="comment">
    <form method="POST">
        {% csrf_token %}
        <fieldset class="form-group" id="login1">
            {{ form|crispy}}
        </fieldset>
        <div class="form-group">
            <button class="btn btn-outline-info" id="loginbtn" type="submit">Comment</button>
        </div>
    </form>
    <h1>Comments {{ post.comments.count }}</h1>
    {% for comment in post.comments.all %}
    <p>{{ comment.author }}</p>
    <p>{{ comment.comment }}</p>
    {% empty %}
    <p> There is no comment</p>
    {% endfor %}
</div>

任何人都可以帮助我尝试用Google搜索仅显示基于功能的视图

0 个答案:

没有答案