如何在Django中获取文件扩展名?

如何在Django中获取文件扩展名?,第1张

如何在Django中获取文件扩展名?

您也可以使用表单中的clean()方法来对其进行验证。因此,您可以拒绝不是mp3的文件。像这样:

class UploadSong(forms.Form):    [...]    def clean(self):        cleaned_data = super(UploadSong, self).clean()        file = cleaned_data.get('file')        if file: filename = file.name print filename if filename.endswith('.mp3'):     print 'File is a mp3' else:     print 'File is NOT a mp3'     raise forms.ValidationError("File is not a mp3. Please upload only mp3 files")        return file


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5664077.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存