在Django中通过Admin上传图像

时间:2013-02-26 00:19:46

标签: python django django-admin django-models

我正在尝试通过Django管理面板上传图片。我在Google App Engine上运行并使用filetransfers插件(不确定是否与我遇到的问题相关)。

来自我的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

来自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)   

我得到的错误是“没有名为Image的模块”。我没有安装PIL。我是否需要安装它才能启用此功能?

更新:这里是导致错误的追溯:

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

2 个答案:

答案 0 :(得分:1)

您需要安装PIL或同等产品。枕头在python 3.x的情况下。如果您在安装时遇到问题,我会得到一些并且很容易修复,您只需要添加python.h的路径即可。也许这是现在修复的...... 没有什么可以在models.py中添加

Link

答案 1 :(得分:0)

是的,你需要安装Pil。但在此之前,您需要将jpeglib安装到您的服务器上(没有此PIL无法使用jpeg文件)

Alterall,你可以包括

from django.db.models import ImageField

在models.py