大家好,我正在制作一个回复表单,用户可以从中回复评论,我使用评论表单两次,第一次是评论本身,第二次是回复。但是当我初始化回复表单时,它给了我模型的数据。它不会显示为表单
这是评论和回复的TextBox
models.py
这是评论和回复的views.py
class CommentManager(models.Manager):
def all(self):
qs = super(CommentManager,self).filter(parent = None)
return qs
class Comment(models.Model):
post = models.ForeignKey(Post,on_delete=models.CASCADE,related_name = "comments")
name = models.CharField(max_length = 200)
body = models.TextField(default = True)
pub_date = models.DateTimeField(auto_now_add = True)
parent = models.ForeignKey('self',null = True,blank = True)
class Meta:
ordering = ['-pub_date']
def __str__(self):
return self.name
def replies(self):
return Reply.objects.filter(parent = self)
@property
def is_parent(self):
if self.parent is not None:
return False
return True
这是评论表单的forms.py
def BlogDetail(request,pk):
post = get_object_or_404(Post,pk = pk)
comment = CommentForm(
request.POST or None)
subscribe = Subscribe()
parent_obj = None
if request.method == 'POST':
subscribe = Subscribe(request.POST)
comment = CommentForm(request.POST)
if comment.is_valid():
comment.instance.post = post
comment.save()
return redirect('index')
try:
parent_id = request.POST.get('parent_id')
except:
parent_id = None
if parent_id:
parent_qs = Comments.objects.filter(id = parent_id)
if parent_qs.exits():
parent_obj = parent_qs.first()
elif subscribe.is_valid():
subscribe = subscribe.save(commit = True)
return redirect('index')
return render(request,'app/blog.html',{'blog_object':post,'comment':comment,
'subscribe':subscribe,'parent':parent_obj,
})
这是评论部分的html
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['name','body',]
widgets = {
'body':forms.Textarea(attrs = {'class':'form-control','placeholder':'You,r comment','minlength':'20'}),
'name': forms.TextInput(attrs = {'class':'form-control','placeholder':'You,r name','minlength':'5'})
}
在评论部分正下方的是html答复
<div class="comments">
<div class="row">
<div class="container">
</div>
<form action="." method="post" id="commentform" class="comment-form" >
{% csrf_token %}
<div class="col">
<div class="form-group form-group-with-icon comment-form-email">
{{comment}}
<div class="form-control-border"></div>
</div>
</div>
<div class="col">
<p class="form-submit">
<input name="submit" type="submit" id="submit" class="submit"value="Post Comment">
</p>
</div>
</form>
</div>
<div class="post-comments">
<h3 style = "color: #ff714a; font-weight:300;">See the latest comments</h3>
{% for comment in blog_object.get_comments %}
<div class="container">
<div class="row">
<div class="col comment_head">
<strong>{{comment.name}}</strong>
<div class="col comment_body">
<p>{{comment.body}}</p>
</div>
</div>
</div>
<div class="border"></div>