Uploading an Image through Admin in Django

2019-06-12 03:21发布

问题:

I am trying to upload an image through the Django Admin Panel. I am running on Google App Engine and using the filetransfers plugin (not sure if either is relevant to the problem I am having).

From my modelys.py:

from django.db import models

# Create your models here.
class CarouselItem (models.Model):
    caption = models.CharField(max_length=200,blank=True)
    order = models.IntegerField()
    carousel_image = models.ImageField(upload_to='uploads/carousel_image/%Y/%m/%d/')
    def __unicode__(self):
        return self.caption

From admin.py:

from carousel.models import CarouselItem
from django.contrib import admin
from imperavi.admin import ImperaviAdmin

class carouselAdmin(ImperaviAdmin):
    list_display = ('caption','order')

admin.site.register(CarouselItem, carouselAdmin)   

The error I am getting is "No module named Image". I don't have PIL installed. Do I need to install it to enable this functionality?

Update: here is traceback that caused error:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/admin/carousel/carouselitem/add/

Django Version: 1.3.1
Python Version: 2.7.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.contenttypes',
 'django.contrib.auth',
 'django.contrib.sessions',
 'djangotoolbox',
 'autoload',
 'dbindexer',
 'django.contrib.staticfiles',
 'portfolio',
 'EBoardMembers',
 'Calendar',
 'filetransfers',
 'positions',
 'imperavi',
 'chunks',
 'carousel',
 'djangoappengine']
Installed Middleware:
('autoload.middleware.AutoloadMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware')


Traceback:
File "/Users/Zach/Documents/Cornell/Activities/MICC/new_website/MICC_app_engine/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/Zach/Documents/Cornell/Activities/MICC/new_website/MICC_app_engine/django/contrib/admin/options.py" in wrapper
  307.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Users/Zach/Documents/Cornell/Activities/MICC/new_website/MICC_app_engine/django/utils/decorators.py" in _wrapped_view
  93.                     response = view_func(request, *args, **kwargs)
File "/Users/Zach/Documents/Cornell/Activities/MICC/new_website/MICC_app_engine/django/views/decorators/cache.py" in _wrapped_view_func
  79.         response = view_func(request, *args, **kwargs)
File "/Users/Zach/Documents/Cornell/Activities/MICC/new_website/MICC_app_engine/django/contrib/admin/sites.py" in inner
  197.             return view(request, *args, **kwargs)
File "/Users/Zach/Documents/Cornell/Activities/MICC/new_website/MICC_app_engine/django/utils/decorators.py" in _wrapper
  28.             return bound_func(*args, **kwargs)
File "/Users/Zach/Documents/Cornell/Activities/MICC/new_website/MICC_app_engine/django/utils/decorators.py" in _wrapped_view
  93.                     response = view_func(request, *args, **kwargs)
File "/Users/Zach/Documents/Cornell/Activities/MICC/new_website/MICC_app_engine/django/utils/decorators.py" in bound_func
  24.                 return func(self, *args2, **kwargs2)
File "/Users/Zach/Documents/Cornell/Activities/MICC/new_website/MICC_app_engine/django/db/transaction.py" in inner
  217.                 res = func(*args, **kwargs)
File "/Users/Zach/Documents/Cornell/Activities/MICC/new_website/MICC_app_engine/django/contrib/admin/options.py" in add_view
  864.             if form.is_valid():
File "/Users/Zach/Documents/Cornell/Activities/MICC/new_website/MICC_app_engine/django/forms/forms.py" in is_valid
  121.         return self.is_bound and not bool(self.errors)
File "/Users/Zach/Documents/Cornell/Activities/MICC/new_website/MICC_app_engine/django/forms/forms.py" in _get_errors
  112.             self.full_clean()
File "/Users/Zach/Documents/Cornell/Activities/MICC/new_website/MICC_app_engine/django/forms/forms.py" in full_clean
  267.         self._clean_fields()
File "/Users/Zach/Documents/Cornell/Activities/MICC/new_website/MICC_app_engine/django/forms/forms.py" in _clean_fields
  282.                     value = field.clean(value, initial)
File "/Users/Zach/Documents/Cornell/Activities/MICC/new_website/MICC_app_engine/django/forms/fields.py" in clean
  503.         return super(FileField, self).clean(data)
File "/Users/Zach/Documents/Cornell/Activities/MICC/new_website/MICC_app_engine/django/forms/fields.py" in clean
  163.         value = self.to_python(value)
File "/Users/Zach/Documents/Cornell/Activities/MICC/new_website/MICC_app_engine/django/forms/fields.py" in to_python
  528.             import Image

Exception Type: ImportError at /admin/carousel/carouselitem/add/
Exception Value: No module named Image

回答1:

You need to install PIL or equivalent. Pillow in case of python 3.x. If you have trouble installing just tell, I've got some and it's easy to fix, you just have to add the path to your python.h . Maybe this is fixed now... Nothing to add in models.py

Link



回答2:

Yes, you need to install Pil. But before that you need to install jpeglib to your server (without this PIL can't work with jpeg files)

Alterall, you can include

from django.db.models import ImageField

in your models.py