Python 如何测试对象是否是django上载文件?
我正在使用django3.1和Python3.7 我有一本字典如下:Python 如何测试对象是否是django上载文件?,python,django,Python,Django,我正在使用django3.1和Python3.7 我有一本字典如下: {'text2': 'text2', 'file2': <UploadedFile: img5.jpg (image/jpeg)>, 'file22': <UploadedFile: img8.jpg (image/jpeg)>} 当我检查的类型时,它会显示: <class 'django.core.files.uploadedfile.UploadedFile'> 我尝试了以下几点
{'text2': 'text2', 'file2': <UploadedFile: img5.jpg (image/jpeg)>, 'file22': <UploadedFile: img8.jpg (image/jpeg)>}
当我检查
的类型时,它会显示:
<class 'django.core.files.uploadedfile.UploadedFile'>
我尝试了以下几点:
data1 = {'text2': 'text2', 'file2': <UploadedFile: img5.jpg (image/jpeg)>, 'file22': <UploadedFile: img8.jpg (image/jpeg)>}
for key in data1:
if isinstance(data1[key], io.IOBase):
print(data1[key].name)
else:
print('not a file')
data1={'text2':'text2','file2':,'file22':}
对于数据1中的密钥:
如果isinstance(data1[key],io.IOBase):
打印(数据1[key].name)
其他:
打印('不是文件')
它总是显示而不是文件
有什么想法吗?谢谢 您可以使用类来检查条件
from django.core.files.uploadedfile import UploadedFile
data = {} # you data
for key, value in data.items():
if isinstance(value, UploadedFile):
print(value.name)
从django.core.files.uploadedfile导入uploadedfile
data={}#你的数据
对于键,data.items()中的值:
如果isinstance(值,UploadedFile):
print(value.name)
为什么要检查io.IOBase类而不是UploadedFile?为什么不直接查看request.Files?在本例中,我无权访问request.Files。我不知道如何上传文件类型:))
from django.core.files.uploadedfile import UploadedFile
data = {} # you data
for key, value in data.items():
if isinstance(value, UploadedFile):
print(value.name)