Only accept a certain file type in FileField, server-side

前端 未结 10 649
日久生厌
日久生厌 2020-11-27 02:56

How can I restrict FileField to only accept a certain type of file (video, audio, pdf, etc.) in an elegant way, server-side?

10条回答
  •  清酒与你
    2020-11-27 03:20

    You can define a list of accepted mime types in settings and then define a validator which uses python-magic to detect the mime-type and raises ValidationError if the mime-type is not accepted. Set that validator on the file form field.

    The only problem is that sometimes the mime type is application/octet-stream, which could correspond to different file formats. Did someone of you overcome this issue?

提交回复
热议问题