擅长:python、mysql、java
<p>您还可以使用表单中的clean()方法来验证它。因此,您可以拒绝不是mp3的文件。像这样的:</p>
<pre><code>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
</code></pre>