Django:如何将表单实例设置为HTML页面中定义的变量



所以我的博客有帖子和评论。我希望用户能够通过BOOTSTRAP MODAL编辑评论,当你点击编辑按钮时,该MODAL会在同一页面内打开。但我实际上面临着一个问题,现在html页面中有一个for循环,我在其中定义了注释变量:

{%for comment in comments%}
<button type="button" class="btn btn-warning btn-sm" data-toggle="modal" data- 
target="#Edit{{comment.id}}Modal">edit test</button> 
{%endfor%}

Views.py:代码

post = Post.objects.get(slug=slug)
comments = post.comment_set.all()
if request.method == 'POST':
editcommentform = CommentForm(request.POST, instance=comment)
if editcommentform.is_valid():
editcommentform.save()
else:
editcommentform = CommentForm(instance=comment)

现在在我的视图.py中,我实际上无法将实例设置为注释,因为我得到了一个错误";注释未定义";

我的问题是,当实例仅在HTML页面的for循环中定义时,如何将其设置为注释

很抱歉,如果这听起来不清楚或奇怪,我是这个领域的新手。

您为按钮输入一个名称,并在视图函数中使用它

{%for comment in comments%}
<button type="button" class="btn btn-warning btn-sm" data-toggle="modal" data- 
target="#Edit{{comment.id}}Modal" name={{comment.id}}>edit test</button> 
{%endfor%}

然后,您可以使函数以comment_id作为参数,并使用它来更新数据库

最新更新