【问题标题】:Django Error (13, 'Permission denied')Django 错误(13,“权限被拒绝”)
【发布时间】:2013-01-25 17:18:35
【问题描述】:

我一直在 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 上创建了“图像”文件夹?

标签: django windows permissions


【解决方案1】:

设置 SELinux 权限 http://wiki.apache.org/httpd/13PermissionDenied 看看这个,可能有解决办法

【讨论】:

    【解决方案2】:

    在我看来,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 部分

    更新

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

    【讨论】:

    • 我正在使用 window vista atm 。我找到了位于 MEDIA_ROOT = 'C:/djcode/mysite/photo/media' 的媒体文件夹,并添加了对权限的完全控制。错误仍然出现。应该怎么做?
    • asermax ,这应该是我的图像文件夹 C:/djcode/mysite/photo/media'
    • 不,你的图片文件夹应该在 C:/djcode/mysite/photo/media/images。你已经在你的 models.py 的 upload_to 参数中说你正在上传图片到这个目录。正如 asermax 正确指出的那样,如果您还没有创建文件夹,则需要创建该文件夹。
    • 哦,好的。我创建了它,但是当我上传图片时,错误仍然出现。谢谢你们试图帮助我。我认为我在制作 django 应用程序方面缺乏知识。我消耗的唯一知识是通过 Google python classes 和 django book 从第 1 章到第 7 章。然后我尝试跳入 lightbird django 示例,但事实证明它很难。 @Aidan Ewen,我看到你对 django 有很好的了解,像我这样的初学者应该如何成功学习 django。是通过制作 django 示例应用程序吗?并试图挑选零碎或回到掌握phython
    • 我添加了一个更新 - 看看是否能解决您的问题。不要气馁。你做的不错。这些事情只是需要时间。如果您还没有完成官方 django 教程,那么您可以尝试一下 - docs.djangoproject.com/en/1.4/intro/tutorial01。只要坚持并继续前进。你会遇到问题,但这是你学到最多的地方。
    猜你喜欢
    • 2018-07-19
    • 2017-04-05
    • 2017-04-04
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 2018-02-05
    • 2017-07-23
    相关资源
    最近更新 更多