Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在django模板中获取文件类型_Django_Django Templates_Django File Upload - Fatal编程技术网

在django模板中获取文件类型

在django模板中获取文件类型,django,django-templates,django-file-upload,Django,Django Templates,Django File Upload,我需要在django模板中获取上传文件的文件类型。 我已经写了一个templatetag来获取文件名,但我还需要文件类型 我可能能够获取文件的内容类型,但在调试时找不到它 doc_list.html {{ d.file|filename }} {{ d.file|filetype }} 模板标签/helper.py register = template.Library() @register.filter def filename(value): return os.path.ba

我需要在django模板中获取上传文件的文件类型。 我已经写了一个templatetag来获取文件名,但我还需要文件类型

我可能能够获取文件的内容类型,但在调试时找不到它

doc_list.html

{{ d.file|filename }}
{{ d.file|filetype }}
模板标签/helper.py

register = template.Library()

@register.filter
def filename(value):
    return os.path.basename(value.file.name)

@register.filter
def filetype(value):
    return ???

返回:

'application/pdf'

你说的“文件类型”到底是什么意思?MIME类型?是的,我需要MIME类型。对不起,我查错了
mime = magic.Magic(mime=True)
mime.from_file("testdata/test.pdf")
'application/pdf'