您也可以使用表单中的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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)