我正在浏览基础django文档,因为我正在尝试为我的旧项目提供一些基本的编辑视图类。为此,我必须使用formsets。我对此示例中用于生成编辑视图的调用模式感到困惑。
您必须使用request.POST实例化验证的formset的确切原因是什么?对于错误,您使用初始实例再次重新创建整个事件...(否则它不会显示任何内容数据)
def manage_books(request, author_id):
author = Author.objects.get(pk=author_id)
BookInlineFormSet = inlineformset_factory(Author, Book, fields=('title',))
if request.method == "POST":
formset = BookInlineFormSet(request.POST, request.FILES, instance=author)
if formset.is_valid():
formset.save()
# Do something. Should generally end with a redirect. For example:
return HttpResponseRedirect(author.get_absolute_url())
else:
formset = BookInlineFormSet(instance=author)
return render(request, 'manage_books.html', {'formset': formset})