How to pass arbitrary Python object (e.g. InMemoryUploadFile) to a different Django view

Question:

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:

  1. Upload: the template contains the form that allows the user to select a file to upload.
  2. Check: the template shows the aggregate statistics and contains a form with buttons to go back or confirm that the uploaded file is correct.
  3. 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.

Asked By: Waldheri

||

Answers:

I see your problem can be solved in two ways:

a) InMemoryUploadFile will be deleted after your view (function) returns something.

  1. User send file on view_reciever, where this file will be processed with your business logic, and saved on the cloud and returned to a user as statistic for confirmation, whether it is good or not.
  2. If file is good, then it is already saved on your cloud.
  3. If it is bad – then delete it.

b) Using Ajax request:

  1. User can send file to a view which will only process your file
    and return statistics via ajax request.
  2. If it is good for user, he can send new ajax request with this file, and it would save file in the cloud.
  3. If it is bad, do nothing (but userfriendly) (it isn’t stored on your cloud anyway).
Answered By: oruchkin
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.