Search code examples
djangodjango-sessions

How to add files in Django session?


I want to get some files from the user and after getting the files, I want the user should make a payment after payment is done, an order should be created and the files should be stored against that order in the Database. I know how to create an order and store the files in a database.

I have used Django's sessions to store the string data. But want to store files also. I have used the following code :

In HTML:

<form method="POST" enctype="multipart/form-data">

       <input type="file" name="filename">

</form>

In views.py :

if request.method == "POST":
    request.session['filename'] = request.FILES['filename']

It throws errors as : Object of type InMemoryUploadedFile is not JSON serializable


Solution

  • This error been thrown because you're not allowed to put non-serializable objects into the session.

    You may encounter this problem by saving it on the server and just save in the session the file name for future usage.

    See more:

    Python and Django - How to use in memory and temporary files