Django错误(13,“权限被拒绝”)

6
我正在开发一个照片组织和分享应用程序,第一部分在http://lightbird.net/dbe/photo.html。我想生成缩略图,但是出现了以下错误。我使用的操作系统是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)

我该如何解决这个错误?

你在 MEDIA_ROOT 上创建了“images”文件夹吗? - asermax
2个回答

1
我觉得django似乎没有访问你的MEDIA_ROOT文件夹所需的权限。
请查看您的settings.py文件中的MEDIA_ROOT设置。然后检查文件夹的权限(例如从bash shell运行类似于“ls -lsa /path/to/media_root”的命令)。确保运行django的用户对该文件夹具有写入权限。
此外,正如asermax指出的那样,请确保在MEDIA_ROOT下创建了一个images目录。
请查看提供静态文件文档,特别是提供其他目录部分。
更新
也许是这个问题。尝试将im.save(tf2.name, "JPEG")替换为im.save(tf2, "JPEG")

我目前正在使用的是Windows Vista系统。我找到了我的媒体文件夹,即MEDIA_ROOT = 'C:/djcode/mysite/photo/media',并已经添加了完全控制权限。但错误仍然出现。我该怎么办? - supersheep1
asermax,这应该是我的图像文件夹C:/djcode/mysite/photo/media吗? - supersheep1
不,你的图像文件夹应该在C:/djcode/mysite/photo/media/images处。你已经在models.py的upload_to参数中指定了上传到这个目录的图像。正如asermax所指出的那样,如果你还没有创建文件夹,你需要创建它。 - Aidan Ewen
好的。我创建了它,但是当我上传图片时,错误仍然出现。谢谢大家尝试帮助我。我认为我在制作Django应用程序方面缺乏知识。我所消耗的唯一知识是通过Google Python课程和Django书籍从第1章到第7章。然后我尝试跳入Lightbird Django示例,但被证明很难。@Aidan Ewen,我看到您对Django有很深的了解,像我这样的初学者应该如何成功学习Django?是通过制作Django示例应用程序并尝试挑选一些要点,还是回到掌握Python? - supersheep1
我已经添加了一个更新 - 看看是否解决了你的问题。不要灰心丧气,你做得很好。这些事情需要时间。如果你还没有完成官方的Django教程,那么可以尝试一下 - https://docs.djangoproject.com/en/1.4/intro/tutorial01/。只要坚持不懈,继续前进。你会遇到问题,但那正是你学到最多的地方。 - Aidan Ewen

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接