我正在测试示例代码验证图像字段(Django custom validation in model form for imagefield (max file size etc.)):
models.py
inline.f137c7f1f4e2a52a2fb9.bundle.js:1 Uncaught SyntaxError: Unexpected token <
polyfills.25e42e2a7a0746e9ff75.bundle.js:1 Uncaught SyntaxError: Unexpected token <
main.0d9f8e7be2ccd1472551.bundle.js:1 Uncaught SyntaxError: Unexpected token <
admin.py
from django.db import models
from filer.fields.image import FilerImageField
class Event(models.Model):
# (...)
banner = FilerImageField(verbose_name='Banner')
注册图像时出现错误,而不是django-admin。版本:Pillow == 4.3.0,Python == 3.6
from django.contrib import admin
from django import forms
from PIL import Image
from .models import Event
class EventForm(forms.ModelForm):
class Meta:
model = Event
fields = '__all__'
def clean_banner(self):
image = self.cleaned_data.get('banner', False)
if image:
img = Image.open(image)
img.load()
width, height = img.size
max_width = 879
max_height = 392
if width > max_width or width < max_width or height > max_height or height < max_height:
raise forms.ValidationError('Imagem está com dimensão incorreta: %s x %s pixels. Insira uma imagem com %s x %s pixels.'
%(width, height, max_width, max_height))
# Máx 3MB
if len(image) > (3 * 1024 * 1024):
raise forms.ValidationError('Arquivo de imagem muito grande (máximo 3MB).')
name_type, ext = image.content_type.split('/')
if not (name_type == 'image' and ext.lower() in ['png', 'jpg', 'jpeg']):
raise forms.ValidationError('Por favor, use imagem em formato JPG, JPEG ou PNG.')
else:
raise forms.ValidationError('Não foi possível ler a imagem carregada.')
return image
我在函数clean_banne中尝试了Traceback (most recent call last):
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\PIL\Image.py", line 2534, in open
fp.seek(0)
AttributeError: 'Image' object has no attribute 'seek'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\contrib\admin\options.py", line 551, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\views\decorators\cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\contrib\admin\sites.py", line 224, in inner
return view(request, *args, **kwargs)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\contrib\admin\options.py", line 1508, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\utils\decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\utils\decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\contrib\admin\options.py", line 1408, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\contrib\admin\options.py", line 1440, in _changeform_view
if form.is_valid():
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\forms\forms.py", line 183, in is_valid
return self.is_bound and not self.errors
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\forms\forms.py", line 175, in errors
self.full_clean()
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\forms\forms.py", line 384, in full_clean
self._clean_fields()
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\forms\forms.py", line 405, in _clean_fields
value = getattr(self, 'clean_%s' % name)()
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\integrafundaj\events\admin.py", line 19, in clean_banner
img = Image.open(image)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\PIL\Image.py", line 2536, in open
fp = io.BytesIO(fp.read())
AttributeError: 'Image' object has no attribute 'read'
,但它不起作用。怎么解决?问题出在python版本中?