0
私のdjangoファイルのアップロードにファイルサイズ& content_typeの制限を実装しようとしています。理想的には、アップロードする前に検証したいと思います。当初はこのコードを使用していますが、動作していません。Djangoのファイルサイズとcontent_typeの制限はフォームレベルかモデルか?
class ContentTypeRestrictedFileField(FileField):
"""
Same as FileField, but you can specify:
* content_types - list containing allowed content_types. Example: ['application/pdf', 'image/jpeg']
* max_upload_size - a number indicating the maximum file size allowed for upload.
2.5MB - 2621440
5MB - 5242880
10MB - 10485760
20MB - 20971520
50MB - 5242880
100MB 104857600
250MB - 214958080
500MB - 429916160
"""
def __init__(self, *args, **kwargs):
self.content_types = kwargs.pop("content_types", None)
self.max_upload_size = kwargs.pop("max_upload_size", None)
super(ContentTypeRestrictedFileField, self).__init__(*args, **kwargs)
def clean(self, *args, **kwargs):
data = super(ContentTypeRestrictedFileField, self).clean(*args, **kwargs)
file = data.file
try:
content_type = file.content_type
if content_type in self.content_types:
if file._size > self.max_upload_size:
raise forms.ValidationError(_('Please keep filesize under %s. Current filesize %s') % (filesizeformat(self.max_upload_size), filesizeformat(file._size)))
else:
raise forms.ValidationError(_('Filetype not supported.'))
except AttributeError:
pass
return data
これまでのところ、まったく動作しません。それは私が通常のFileFieldを使用しているようなものです。私はビューでそれを行う場合 しかし、私はそれがフォームレベルのすなわちで仕事を得ることができます:
if form.is_valid():
file_name = request.FILES['pdf_file'].name
size = request.FILES['pdf_file'].size
content = request.FILES['pdf_file'].content_type
### Validate Size & ConTent here
new_pdf = PdfFiles(pdf_file = request.FILES['pdf_file'])
new_pdf.save()
これを行うための最も好ましい方法は何を?