Django更正为POST /表单验证

时间:2021-12-22 15:59:29

I would like to upload a file with data to Django, and use django's forms to validate the data.

我想将带有数据的文件上传到Django,并使用django的表单来验证数据。

My idea was to have two views:

我的想法是有两种观点:

  1. A form where the user selecte the upload file The file is uploaded to thy server
  2. 用户选择上载文件的表单将文件上载到您的服务器
  3. When the file successfully we are redirected to a from processing view, where the data in the file is used to populate the form and process it.
  4. 当文件成功后,我们将被重定向到处理视图,其中文件中的数据用于填充表单并对其进行处理。

According to this anser, you can't POST data with a redirect: Django: How do I redirect a post and pass on the post data

根据此anser,您无法使用重定向POST数据:Django:如何重定向帖子并传递帖子数据

Is there a common pattern to achieve this sort of behaviour?

有没有一种共同的模式来实现这种行为?

I could easily use the initail kwarg to the form / view to populate it but the user needs to submit the from from the second page. I would like to process the form from the get request without the user needing to manually submit the initial form (a redirect with POST essentially).

我可以轻松地使用initail kwarg到表单/视图来填充它,但用户需要从第二页提交from。我想从get请求处理表单,而无需用户手动提交初始表单(基本上是POST的重定向)。

(If this can be cleanly achived with generic class based views, all the better).

(如果可以通过基于通用类的视图干净地实现这一点,那就更好了)。

1 个解决方案

#1


1  

The simple way to do this would be to extract the data in the first view and store it in the session. The second view would then simply retrieve the data from the session and use it to populate the form.

执行此操作的简单方法是在第一个视图中提取数据并将其存储在会话中。然后,第二个视图将简单地从会话中检索数据并使用它来填充表单。

#1


1  

The simple way to do this would be to extract the data in the first view and store it in the session. The second view would then simply retrieve the data from the session and use it to populate the form.

执行此操作的简单方法是在第一个视图中提取数据并将其存储在会话中。然后,第二个视图将简单地从会话中检索数据并使用它来填充表单。