Sunday, 15 March 2015

django - Using StringIO With Python 3.4 -



django - Using StringIO With Python 3.4 -

so i'm using python 3.4 code isn't working. importing cstringio , i've fixed it's getting unexpected error.

heres error:

traceback: file "/home/callumb/webapps/photo/lib/python3.4/django-1.7-py3.4.egg/django/core/handlers/base.py" in get_response 111. response = wrapped_callback(request, *callback_args, **callback_kwargs) file "/home/callumb/webapps/photo/lib/python3.4/django-1.7-py3.4.egg/django/contrib/admin/options.py" in wrapper 567. homecoming self.admin_site.admin_view(view)(*args, **kwargs) file "/home/callumb/webapps/photo/lib/python3.4/django-1.7-py3.4.egg/django/utils/decorators.py" in _wrapped_view 105. response = view_func(request, *args, **kwargs) file "/home/callumb/webapps/photo/lib/python3.4/django-1.7-py3.4.egg/django/views/decorators/cache.py" in _wrapped_view_func 52. response = view_func(request, *args, **kwargs) file "/home/callumb/webapps/photo/lib/python3.4/django-1.7-py3.4.egg/django/contrib/admin/sites.py" in inner 204. homecoming view(request, *args, **kwargs) file "/home/callumb/webapps/photo/lib/python3.4/django-1.7-py3.4.egg/django/contrib/admin/options.py" in add_view 1437. homecoming self.changeform_view(request, none, form_url, extra_context) file "/home/callumb/webapps/photo/lib/python3.4/django-1.7-py3.4.egg/django/utils/decorators.py" in _wrapper 29. homecoming bound_func(*args, **kwargs) file "/home/callumb/webapps/photo/lib/python3.4/django-1.7-py3.4.egg/django/utils/decorators.py" in _wrapped_view 105. response = view_func(request, *args, **kwargs) file "/home/callumb/webapps/photo/lib/python3.4/django-1.7-py3.4.egg/django/utils/decorators.py" in bound_func 25. homecoming func.__get__(self, type(self))(*args2, **kwargs2) file "/home/callumb/webapps/photo/lib/python3.4/django-1.7-py3.4.egg/django/db/transaction.py" in inner 394. homecoming func(*args, **kwargs) file "/home/callumb/webapps/photo/lib/python3.4/django-1.7-py3.4.egg/django/contrib/admin/options.py" in changeform_view 1388. self.save_model(request, new_object, form, not add) file "/home/callumb/webapps/photo/lib/python3.4/django-1.7-py3.4.egg/django/contrib/admin/options.py" in save_model 1029. obj.save() file "/home/callumb/webapps/photo/app/images/models.py" in save 77. self.create_thumbnail() file "/home/callumb/webapps/photo/app/images/models.py" in create_thumbnail 50. image = image.open(stringio(self.image.read())) exception type: typeerror @ /admin/images/image/add/ exception value: initial_value must str or none, not bytes

and here code:

def create_thumbnail(self): if not self.image: homecoming pil import image io import stringio, bytesio django.core.files.uploadedfile import simpleuploadedfile import os thumbnail_size = (280, 600) django_type = self.image.file.content_type if django_type == 'image/jpeg': pil_type = 'jpeg' file_extension = 'jpg' elif django_type == 'image/png': pil_type = 'png' file_extension = 'png' image = image.open(stringio(self.image.read())) image.thumbnail(thumbnail_size, image.antialias) temp_handle = stringio() image.save(temp_handle, pil_type) temp_handle.seek(0) suf = simpleuploadedfile(os.path.split(self.image.name)[-1], temp_handle.read(), content_type=django_type) self.thumbnail.save( '%s_thumbnail.%s' % (os.path.splitext(suf.name)[0], file_extension), suf, save=false )

the traceback telling stringio expects string providing bytes. utilize bytesio instead os stringio.

python django io python-3.4

No comments:

Post a Comment