This question will likely betray my inexperience in web development, so please let me know if I'm solving entirely the wrong problem.
I'm building a web application where users are asked to upload a data file. After uploading, the users are shown some aggregate statistics of the data contained in the file to help catch any errors. The user can then confirm this is the right data. Only then will the file will be stored somewhere on other people's computers in the cloud.
I Django, I have defined three views/templates:
- Upload: the template contains the form that allows the user to select a file to upload.
- Check: the template shows the aggregate statistics and contains a form with buttons to go back or confirm that the uploaded file is correct.
- Confirm: the page shows that the file was stored.
The problem is that the file is uploaded in the Upload view/template; but I only want to determine the aggregate statistics on the Check page and store the file after the user has confirmed its aggregate statistics in the Check view/template. I'm not sure how I can pass the file (which will be an InMemoryUploadFile
object) from the Upload view to the Check view.
Perhaps I'm trying to solve this the wrong way and I should find out a way to do the Check on the same page as the Upload form.
Edit: I have a working solution that feels hacky and not like the right way. Since the data file contains text, I can read its contents and store it in a string. In the Upoad view, I can add this string to the session by storing it in request.session["file_contents"]
. This request
is then passed to the next view when I redirect
from the Upload view to the Check view.
CodePudding user response:
I see your problem can be solved in two ways:
a) InMemoryUploadFile
will be deleted after your view (function)
returns something
- user send file on
view_reciever
, where this file will be processed with your business logic, and saved on thecloud
and returned to a user as statistic for confirmation, whether it is good or not - if file is good, then it is already saved on your cloud
- if it is bad - then delete it
b) using Ajax
request
- user can send file to a
view
which will only process your file andreturn statistics
viaajax request
- if it is good for user, he can send new
ajax request
with this file, and it would save file in the cloud - if it is bad,
do nothing (but userfriendly)
(it isn't stored on your cloud anyway)