gpt4 book ai didi

python - 在Django中,如何确保我的表单在提交后保持其初始值但由于错误而返回到模板?

转载 作者:太空宇宙 更新时间:2023-11-04 01:41:30 25 4
gpt4 key购买 nike

假设有人提交了一个表单。它没有通过 is_valid(),我将它们返回给模板。

如何在不让用户重新输入内容的情况下再次显示表单字段?

我不想使用 { initial: } 选项。

最佳答案

您可以像这样实现您的 View :

def showForm(request):
if request.method == "POST":
form = MyForm(request.POST)
if form.is_valid():
# do stuff on success, like redirection
else:
form = MyForm()

return render_to_response(...,
{"form" : form})
# then in the template, use {{ form.as_table }}, for example

关于python - 在Django中,如何确保我的表单在提交后保持其初始值但由于错误而返回到模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4358001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com