【发布时间】:2021-04-27 22:10:54
【问题描述】:
我正在构建一个 BlogApp 并且我构建了一个功能,如果用户禁用 cmets(通过布尔字段),那么 cmets 将被禁用。但我被困在一个错误上。当我打开浏览器时,它会显示给我:-
赋值前引用的局部变量“comment_form”
views.py
def detail_view(request,id):
data = get_object_or_404(Post,id=id)
comments = data.comments.order_by('-created_at')
new_comment = None
if Post.allow_comments == True:
if request.method == 'POST':
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
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 )
我不知道。我错过了什么。
【问题讨论】:
-
当
Post.allow_comments是False时,comment_form永远不会被定义。 -
request.POST 在你的 if 语句中,放在外面
标签: python html django django-models django-views