如何在Django会话中添加文件?

发布于 2025-02-02 05:52:37 字数 498 浏览 1 评论 0原文

我想从用户那里获取一些文件,并在获取文件后,我希望用户应在付款后付款,应创建订单,并且应该在数据库中存储文件。我知道如何创建订单并将文件存储在数据库中。

我已经使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬色的秋千 2025-02-09 05:52:37

由于您不允许将非序列化对象放入会话,因此引发了此错误。

您可以通过将其保存在服务器上并将其保存在会话中的文件名来遇到此问题。

请参见更多:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文