skip to Main Content

Django Version: 3.1.6
Python Version: 3.6.9

I’m trying to use the ImageField in my django admin page to upload an image. When I try to upload it from my NGinx/Gunicorn server I get this error. However when I run it from port 8000 using manage.py runserver 0:8000 from the SAME server, it works. I don’t understand this error. I have Pillow installed.

Environment:


Request Method: POST
Request URL: http://54.188.59.209/admin/core/employee/add/

Django Version: 3.1.6
Python Version: 3.6.9
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'core']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.6/dist-packages/django/contrib/admin/options.py", line 614, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/django/contrib/admin/sites.py", line 233, in inner
    return view(request, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/django/contrib/admin/options.py", line 1653, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
  File "/usr/local/lib/python3.6/dist-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/django/contrib/admin/options.py", line 1534, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "/usr/local/lib/python3.6/dist-packages/django/contrib/admin/options.py", line 1573, in _changeform_view
    form_validated = form.is_valid()
  File "/usr/local/lib/python3.6/dist-packages/django/forms/forms.py", line 177, in is_valid
    return self.is_bound and not self.errors
  File "/usr/local/lib/python3.6/dist-packages/django/forms/forms.py", line 172, in errors
    self.full_clean()
  File "/usr/local/lib/python3.6/dist-packages/django/forms/forms.py", line 374, in full_clean
    self._clean_fields()
  File "/usr/local/lib/python3.6/dist-packages/django/forms/forms.py", line 390, in _clean_fields
    value = field.clean(value, initial)
  File "/usr/local/lib/python3.6/dist-packages/django/forms/fields.py", line 598, in clean
    return super().clean(data)
  File "/usr/local/lib/python3.6/dist-packages/django/forms/fields.py", line 149, in clean
    value = self.to_python(value)
  File "/usr/local/lib/python3.6/dist-packages/django/forms/fields.py", line 627, in to_python
    from PIL import Image

Exception Type: ModuleNotFoundError at /admin/core/employee/add/
Exception Value: No module named 'PIL'

3

Answers


  1. Pil is not available as of now in django.
    Use its successor

    pip install pillow

    As you said you have pillow installed then the problem must be with pip , upgrade the pip and then try out if it works .

    pip install –upgrade pip

    the version of pip does dictate how packages are unzipped, which ultimately determines whether or not modules are loaded properly.
    Then import image and hope that works.

    Login or Signup to reply.
  2. Django does not came with Pillow preinstalled.
    To install, simply run:

    python3 -m pip install --upgrade Pillow
    

    to install or upgrade.

    Login or Signup to reply.
  3. Try to use these Command :-

    1). If you’re using Unix then run .

    python -m pip install -U pip

    2). This Command is for install or Upgrade setup tools. (Setup tools are install by default but you should upgrade it)

    pip install setuptools

    3). AND now, uninstall and reinstall the Pillow.

    For Uninstall

    pip uninstall pillow

    Now install

    pip install pillow

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search