[英]data How to keep form when user gets redirected back to the form when they fail a validation (Python, Django)?
I know this might be a duplicate question, but the previous one was an older question and those questions uses a form instance which doesn't really help me. 我知道这可能是一个重复的问题,但是上一个问题是一个较旧的问题,这些问题使用了对我没有帮助的表单实例。
How do I keep my form data after a failed validation? 验证失败后如何保存表单数据? I have multiple dropdowns and input fields and I hate to see my users re-do and re-type everything when they fail validation in the backend. 我有多个下拉菜单和输入字段,但我讨厌看到我的用户在后端验证失败时重新做并重新键入所有内容。 Let's say I have this very simple form: 假设我有一个非常简单的表格:
HTML: HTML:
<form class="" action="/register" method="post">
<label for="">First Name</label>
<input type="text" name="" value="">
<label for="">Last Name</label>
<input type="text" name="" value="">
<label for="">Password</label>
<input type="password" name="" value="">
</form>
views.py: views.py:
def register(self):
.....
if errors:
for err in errors
messages.errors(request, err)
return redirect('/')
else:
messages.success(request, "Welcome User!")
return redirect('/dashboard')
Most examples that I came across were using the form instance which uses form.save() etc. I opted out on that one. 我遇到的大多数示例都是使用form实例,该实例使用form.save()等。我选择退出该实例。 Is there a way to auto-populate my form with the data that the user submitted if they were to fail validation? 如果用户验证失败,是否可以用用户提交的数据自动填充表单? Thanks! 谢谢!
Django form classes are the way to go. Django表单类是必经之路。 Form validation and rendering are the tasks they were build for. 表单验证和呈现是它们构建的任务。 I would strongly recommend using them, because they also take care of security aspects, when it comes to passing user input back to the browser (all input from user land is evil!). 我强烈建议使用它们,因为当涉及到将用户输入传递回浏览器时,它们还涉及安全方面的问题(来自用户领域的所有输入都是邪恶的!)。
If you really need to achieve this without form classes, you need to add the form values to your rendering context manually - this allows you to use them in your template. 如果确实需要在没有表单类的情况下实现此目的,则需要手动将表单值添加到呈现上下文中-这使您可以在模板中使用它们。
The main problem with your approach is, that you want to redirect in case of validation error. 您的方法的主要问题是,要在验证错误的情况下进行重定向。 A redirect is a response to the browser that tells: I have nothing for you, please go to this location. 重定向是对浏览器的响应,告诉您:我没有什么适合您,请转到此位置。 Usually the browser does not post the data send in the first request also to the second one (which is generally a good behavior). 通常,浏览器不会将在第一个请求中发送的数据也发布到第二个请求中(这通常是一个好习惯)。 You may work around that by answering with status code 307 instead of 302. Read eg Response.Redirect with POST instead of Get? 您可以通过回答状态代码307(而不是302)来解决此问题。例如,阅读Response.Redirect(使用POST而不是Get?)? for more information. 欲获得更多信息。 Alternatively you may encode your form data into the target location using get parameters. 或者,您可以使用get参数将表单数据编码到目标位置。
Again: You should have a very good reason to not just use the django approach of one view that acts on GET and POST different and handles the form properly using the form instance. 再说一遍:您应该有一个很好的理由,不仅要使用对GET和POST起作用的一个视图的django方法,而且要使用表单实例正确处理表单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.