如何在Django会话中添加文件?
我想从用户那里获取一些文件,并在获取文件后,我希望用户应在付款后付款,应创建订单,并且应该在数据库中存储文件。我知道如何创建订单并将文件存储在数据库中。
我已经使用Django的会话来存储字符串数据。但也想存储文件。我使用了以下代码:
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']
它会将错误归为: 类型Inmemoryuploadedfile的对象不是JSON序列化
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您不允许将非序列化对象放入会话,因此引发了此错误。
您可以通过将其保存在服务器上并将其保存在会话中的文件名来遇到此问题。
请参见更多:
Python和Django-如何在内存和临时文件中使用
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