C
C
ckatala2019-06-13 22:37:03
Django
ckatala, 2019-06-13 22:37:03

How to fix not enough arguments for format string?

Updated easy-thumbnails to latest version and stopped working!

from django.http import HttpResponsePermanentRedirect, HttpResponseNotFound, HttpResponseBadRequest
from django.core.files.storage import default_storage
from easy_thumbnails.files import get_thumbnailer
from easy_thumbnails.exceptions import InvalidImageFormatError
import re

SIZE_RE = re.compile(r'^(\d+)x(\d+)$')

def resize(request, path):
  thumbnail_opts = {}
  if 'size' in request.GET:
    if SIZE_RE.match(request.GET['size']):
      thumbnail_opts['size'] = map(int, request.GET['size'].split('x'))
      if 'crop' in request.GET:
        thumbnail_opts['crop'] = request.GET['crop']
    else:
      return HttpResponseBadRequest('Неверный формат. ')
  else:
    return HttpResponsePermanentRedirect(default_storage.url(path))
  try:
    thumbnailer = get_thumbnailer(default_storage, path)
    thumbnail = thumbnailer.get_thumbnail(thumbnail_opts)
    return HttpResponsePermanentRedirect(thumbnail.url)
  except IOError:
    return HttpResponseNotFound('Файл не найден')
  except InvalidImageFormatError:
    return HttpResponseBadRequest('Файл не является изображением.')

Mistake
Traceback (most recent call last):
File "/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/apps/users/api/views.py", line 41, in resize
    thumbnail = thumbnailer.get_thumbnail(thumbnail_opts)
  File "/venv/lib/python3.7/site-packages/easy_thumbnails/files.py", line 512, in get_thumbnail
    thumbnail = self.get_existing_thumbnail(thumbnail_options)
  File "/venv/lib/python3.7/site-packages/easy_thumbnails/files.py", line 473, in get_existing_thumbnail
    high_resolution=high_resolution)
  File "/venv/lib/python3.7/site-packages/easy_thumbnails/files.py", line 436, in get_thumbnail_name
    prepared_opts = thumbnail_options.prepared_options()
  File "/venv/lib/python3.7/site-packages/easy_thumbnails/options.py", line 20, in prepared_options
prepared_opts = ['%sx%s' % tuple(self['size'])]
TypeError: not enough arguments for format string

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2019-06-13
@ckatala

The problem is probably that in the regular expression the numbers are separated by a comma, and you are trying to split them by 'x'. Accordingly, thumbnail_opts['size']you get one element instead of the expected two.

D
Dimonchik, 2019-06-13
@dimonchik2013

throw arguments
or fix the code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question