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

前端 未结 10 684
日久生厌
日久生厌 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:36

    after I checked the accepted answer, I decided to share a tip based on Django documentation. There is already a validator for use to validate file extension. You don't need to rewrite your own custom function to validate whether your file extension is allowed or not.

    https://docs.djangoproject.com/en/3.0/ref/validators/#fileextensionvalidator

    Warning

    Don’t rely on validation of the file extension to determine a file’s type. Files can be renamed to have any extension no matter what data they contain.

提交回复
热议问题