2013-02-10 138 views
5

我一直在处理这个照片管理器和共享应用程序第一部分http://lightbird.net/dbe/photo.html。我试图生成一个缩略图,当我这样做。我得到这个错误。Django错误(13,'Permission denied')

我有Windows Vista。

IOError at /admin/photo/image/add/ 
    (13, 'Permission denied') 
    Request Method: POST 
    Request URL: http://127.0.0.1:8000/admin/photo/image/add/ 
    Django Version: 1.4.3 
    Exception Type: IOError 
    Exception Value: (13, 'Permission denied') 

    Exception Location:C:\Python26\lib\site-packages\PIL\Image.py in save, line 1399 
    Python Executable:C:\Python26\python.exe 
    Python Version: 2.6.0 
    Python Path: 

    ['C:\\djcode\\mysite', 
    'C:\\Python26\\python26.zip', 
    'C:\\Python26\\DLLs', 
    'C:\\Python26\\lib', 
    'C:\\Python26\\lib\\plat-win', 
    'C:\\Python26\\lib\\lib-tk', 
    'C:\\Python26', 
    'C:\\Python26\\lib\\site-packages', 
    'C:\\Python26\\lib\\site-packages\\PIL'] 

    Server time: Sun, 10 Feb 2013 23:49:34 +1100 

models.py

from django.db import models 
from django.contrib.auth.models import User 
from django.contrib import admin 
from string import join 
from django.core.files import File 
from os.path import join as pjoin 
from tempfile import * 

import os 
from PIL import Image as PImage 
from mysite.settings import MEDIA_ROOT 


class Album(models.Model): 
    title = models.CharField(max_length=60) 
    public = models.BooleanField(default=False) 
    def __unicode__(self): 
     return self.title 

class Tag(models.Model): 
    tag = models.CharField(max_length=50) 
    def __unicode__(self): 
     return self.tag 

class Image(models.Model): 
    title = models.CharField(max_length=60, blank=True, null=True) 
    image = models.FileField(upload_to="images/") 
    tags = models.ManyToManyField(Tag, blank=True) 
    albums = models.ManyToManyField(Album, blank=True) 
    created = models.DateTimeField(auto_now_add=True) 
    rating = models.IntegerField(default=50) 
    width = models.IntegerField(blank=True, null=True) 
    height = models.IntegerField(blank=True, null=True) 
    user = models.ForeignKey(User, null=True, blank=True) 
    thumbnail2 = models.ImageField(upload_to="images/", blank=True, null=True) 

    def __unicode__(self): 
     return self.image.name 
    def save(self, *args, **kwargs): 
     """Save image dimensions.""" 
     super(Image, self).save(*args, **kwargs) 
     im = PImage.open(pjoin(MEDIA_ROOT, self.image.name)) 
     self.width, self.height = im.size 

     # large thumbnail 
     fn, ext = os.path.splitext(self.image.name) 
     im.thumbnail((128,128), PImage.ANTIALIAS) 
     thumb_fn = fn + "-thumb2" + ext 
     tf2 = NamedTemporaryFile() 
     im.save(tf2.name, "JPEG") 
     self.thumbnail2.save(thumb_fn, File(open(tf2.name)), save=False) 
     tf2.close() 

     # small thumbnail 
     im.thumbnail((40,40), PImage.ANTIALIAS) 
     thumb_fn = fn + "-thumb" + ext 
     tf = NamedTemporaryFile() 
     im.save(tf.name, "JPEG") 
     self.thumbnail.save(thumb_fn, File(open(tf.name)), save=False) 
     tf.close() 

     super(Image, self).save(*args, ** kwargs) 

    def size(self): 
     """Image size.""" 
     return "%s x %s" % (self.width, self.height) 

    def __unicode__(self): 
     return self.image.name 

    def tags_(self): 
     lst = [x[1] for x in self.tags.values_list()] 
     return str(join(lst, ', ')) 

    def albums_(self): 
     lst = [x[1] for x in self.albums.values_list()] 
     return str(join(lst, ', ')) 

    def thumbnail(self): 
     return """<a href="/media/%s"><img border="0" alt="" src="/media/%s" height="40" /></a>""" % (
                   (self.image.name, self.image.name)) 
    thumbnail.allow_tags = True 
class AlbumAdmin(admin.ModelAdmin): 
    search_fields = ["title"] 
    list_display = ["title"] 

class TagAdmin(admin.ModelAdmin): 
    list_display = ["tag"] 

class ImageAdmin(admin.ModelAdmin): 
    search_fields = ["title"] 
    list_display = ["__unicode__", "title", "user", "rating", "size", "tags_", "albums_","thumbnail", "created"] 
    list_filter = ["tags", "albums"] 
def save_model(self, request, obj, form, change): 
    obj.user = request.user 
    obj.save() 

的问题是在这里:

from django.core.files import File 
from os.path import join as pjoin 
from tempfile import * 

class Image(models.Model): 
    # ... 

    thumbnail2 = models.ImageField(upload_to="images/", blank=True, null=True) 

def save(self, *args, **kwargs): 
    """Save image dimensions.""" 
    super(Image, self).save(*args, **kwargs) 
    im = PImage.open(pjoin(MEDIA_ROOT, self.image.name)) 
    self.width, self.height = im.size 

    # large thumbnail 
    fn, ext = os.path.splitext(self.image.name) 
    im.thumbnail((128,128), PImage.ANTIALIAS) 
    thumb_fn = fn + "-thumb2" + ext 
    tf2 = NamedTemporaryFile() 
    im.save(tf2.name, "JPEG") 
    self.thumbnail2.save(thumb_fn, File(open(tf2.name)), save=False) 
    tf2.close() 

    # small thumbnail 
    im.thumbnail((40,40), PImage.ANTIALIAS) 
    thumb_fn = fn + "-thumb" + ext 
    tf = NamedTemporaryFile() 
    im.save(tf.name, "JPEG") 
    self.thumbnail.save(thumb_fn, File(open(tf.name)), save=False) 
    tf.close() 

    super(Image, self).save(*args, ** kwargs) 

如何解决这个问题?

+0

您是否制作上'MEDIA_ROOT'的“图片”文件夹中的解决方案吗? – asermax 2013-02-10 15:44:20

回答

1

它在我看来像django没有权限它需要访问您的MEDIA_ROOT文件夹。

看看你的settings.py文件中的MEDIA_ROOT设置。然后检查文件夹的权限(类似于bash shell中的ls -lsa /path/to/media_root)。确保用户运行django作为文件夹的写入权限。

此外,正如asermax指出的,确保您在MEDIA_ROOT中创建了一个图像目录。

看一看为serving static files特别是部分中的文档上serving other directories

UPDATE

也许是this issue。尝试用im.save(tf2, "JPEG")

+0

我正在使用window vista atm。我找到了位于MEDIA_ROOT ='C:/ djcode/mysite/photo/media'的媒体文件夹,并且我完全控制了权限。该错误仍然出现。应该做什么? – supersheep1 2013-02-10 13:59:28

+0

asermax,这是假设我的图像文件夹C:/ djcode/mysite/photo/media' – supersheep1 2013-02-10 20:05:39

+0

不,您的图像文件夹应该在C:/ djcode/mysite/photo/media/images。你已经在models.py的upload_to参数中表示你正在将图像上传到这个目录。正如asermax正确指出的那样,如果您尚未创建文件夹,您将需要创建该文件夹。 – 2013-02-10 22:32:39

相关问题