Comment form
is inside your if statement
, if post.allow is false
your form is not callable.
Change for the following :
def detail_view(request,id):
data = get_object_or_404(Post,id=id)
comments = data.comments.order_by('-created_at')
new_comment = None
comment_form = CommentForm(data=request.POST)
if Post.allow_comments == True:
if request.method == 'POST':
if comment_form.is_valid():
comment_form.instance.post_by = data
comment_form.instance.commented_by = request.user
comment_form.instance.active = True
new_comment = comment_form.save()
return redirect('mains:detail_view',id=id)
else:
comment_form = CommentForm()
else:
print("Comments are Disabled")
context = {'data':data,'comments':comments,'new_comment':new_comment,'comment_form':comment_form}
return render(request, detail.html', context )
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…