简体   繁体   English

数据如何在验证失败时将用户重定向回表单(Python,Django)时如何保留表单?

[英]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.

相关问题 验证失败时,Django表单上下文数据丢失 - Django Form context data lost on validation fail Django中的enctype =“ multipart / form-data”表单验证失败 - form validation fail in django with enctype=“multipart/form-data” 为什么django表单即使验证失败也正在更新用户对象 - why is django form is updating user object even when the validation fails 在任何浏览器上单击后退按钮时,表单中的数据都会丢失(Django) - lose data in the form when clicking the back button on any browser (Django) 使用Ajax张贴Django表单数据时呈现验证错误 - Rendering validation errors when using Ajax to POST Django form data Django数据验证格式(admin / auth / user) - Django data validation in form (admin/auth/user) 当提交使用CSS自定义表单验证失败时,如何保留用户在Django表单字段中键入的信息? - How do I preserve the information keyed in by the user in Django form fields when the submission fails validation while using a CSS customized form? Django形式的“软”验证,发回用户以明确确认可疑数据 - Django form “soft” validation, send back to user for explicit confirmation of doubtful data 如何防止浏览器在Django中导航时重新填充表单数据 - How to prevent browser from refilling form data when navigating back in Django Python在django验证之前编辑表单数据 - Python edit form data prior to django validation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM