英文:
Django: Correct way of passing data object from view to template to view?
问题
从一个模板中,用户可以上传一个CSV文件,然后它会被解析,如下所示:
def parseCSV(request):
在这里执行一些魔法操作(符合日期格式等有趣的事情)
返回列名给模板
这个视图返回一个列的列表,然后要求用户选择 x 列进行保存。用户的选择会被提交到以下视图:
def saveCSV(request):
保存逻辑
现在我的问题是,如何在视图1和视图2之间最正确地处理CSV数据对象?是将它保存为临时文件,还是作为数据对象来回传递视图1->模板->视图2?或者可能是其他方式?
英文:
From a template users can upload a csv file which gets parsed in
def parseCSV(request):
magic happens here (conforming date formats and all such fun things)
return column names to template
This view returns a list of columns and the user is asked to pick x columns to save.
The users choice is posted to
def saveCSV(request):
logic for saving
Now my question is, how do I most correctly handle the csv data object between view 1 and 2? Do i save it as a temperary file or do i send it back and forth view1->template->view2 as a data object? Or maybe something third?
答案1
得分: 0
我想在前端解析CSV文件,并为用户提供选择列的选项。在选择列后,我会将这些列和值发送到后端。
英文:
I would like to parse the CSV file at the frontend and give an option to user to choose columns. After choosing columns, I would send these columns with value to Backend.
答案2
得分: 0
没有一个“正确”的方法,一切都取决于具体情况。在这种情况下,它取决于来自CSV文件的数据大小。考虑到数据相当庞大,最佳方法很可能是将解析后的数据存储在服务器上,然后在下一个请求中仅发送用户选择的完整数据集。
我建议您解析数据并将其存储为数据库中的JSON blob,以便您可以轻松地在下一个请求中检索它。这样,您可以发送用户选择的行和列(或“坐标”),然后将其保存为真实数据。立即存储的好处是用户可以在离开流程后返回该流程。然而,缺点是如果用户从不完成流程,您会保存未使用的数据,可能需要稍后清除。如果将其存储在仅包含临时数据的表中,应该会简化清理过程。
英文:
There is no "correct" way as it all depends on the concrete situation. In this case, it depends on the size of the data from the CSV file. Given that the data is rather large, the best approach is most likely to store the parsed data on the server, and then in the next request only send the user's selection of the full data set.
I would suggest you to parse the data and store it as a JSON blob in the database, so that you can easily retrieve it for the next request. This way you can send the user's selection of rows and columns (or "coordinates"), and save that as real data afterwards. The benefit of storing it right away is that the user can return to the process even after leaving the flow. The downside is, though, that you save unused data, if the user never completes the process, and you might need to clear this later. If you store it in a table containing only temporary data, it should ease the cleaning process.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论