[英]How to keep fields values after submit in Django?
I have a form and I would like to keep the values of the fields after a click on submit.我有一个表单,我想在单击提交后保留字段的值。 How can I do this?
我怎样才能做到这一点?
Views.py:视图.py:
if request.method == "POST":
form = MyForm(request.POST)
if form.is_valid():
form.save()
else:
form = MyForm()
template.html模板.html
<form method="post" id="Myform" >
{{form}}
<button class="btn btn-primary" type="submit" >
</form>
Moving initial form assignment outside the check for the POST method should do.应该将初始表单分配移到 POST 方法的检查之外。
form = MyForm(request.POST or None)
if request.method == "POST":
if form.is_valid():
form.save()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.