中查找文件路径Django 对象

发布于 2024-08-31 04:57:20 字数 240 浏览 0 评论 0原文

我有一个 Django 应用程序,提交包后,应该返回其中的值。 将表单提交到名为“insert”的视图:

request.FILES['file']

返回文件对象,但其类型为 << InMemoryUploadedFile>。 我需要的是一种获取上传文件的绝对路径的方法,以便我可以将其提供给一个返回所需值的方法

有人知道我如何实现这一点吗?

谢谢

I have a Django app which, submitting a package, should return values that are inside it..
Submitted the form to a view called "insert":

request.FILES['file']

returns the file objects, but it is of kind < InMemoryUploadedFile>.
What i need is a way to get the absolute path of the uploaded file, so that i can feed it to a method that will return the values needed

Anyone know how i can accomplish this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

天暗了我发光 2024-09-07 04:57:20

当然,名称“InMemory”暗示该文件仅存在于内存中,因此没有路径?

Surely the name "InMemory" is a clue that the file exists in memory only, so doesn't have a path?

生寂 2024-09-07 04:57:20

丹尼尔·罗斯曼(Daniel Roseman)对于上下文来说是正确的。但是,如果您已经使用 .create(...)obj.save() 创建/插入对象到数据库中。可以使用obj..name检索图像的路径。
示例:

假设您有一个带有图像字段的模型。

my_image = models.ImageFiled(upload_to='my_upload_dir')

所以,

obj = ImageModel.objects.create(image=request.FILES['image']) # let's insert it to db
image_path = obj.image.name

# will return 
'my_upload_dir/myimagename.jpg'

Daniel Roseman is correct for the context. But if you already created/inserted the object into the database using .create(...) or obj.save(). The path of the image can be retrieved by using obj.<field_name>.name.
Example:

Suppose you have a model with an image field.

my_image = models.ImageFiled(upload_to='my_upload_dir')

so,

obj = ImageModel.objects.create(image=request.FILES['image']) # let's insert it to db
image_path = obj.image.name

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