簡體   English   中英

Django錯誤(13,'權限被拒絕')

[英]Django Error (13, 'Permission denied')

我一直在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)

我該如何解決這個錯誤?

在我看來,django沒有訪問MEDIA_ROOT文件夾所需的權限。

查看settings.py文件中的MEDIA_ROOT設置。 然后檢查文件夾的權限(類似於來自bash shell的ls -lsa /path/to/media_root )。 確保用戶運行django作為該文件夾的寫入權限。

此外,正如asermax指出的那樣,請確保在MEDIA_ROOT中創建了一個images目錄。

查看提供靜態文件的文檔,特別是有關提供其他目錄的部分

UPDATE

也許就是這個問題 嘗試用im.save(tf2.name, "JPEG")替換im.save(tf2.name, "JPEG") im.save(tf2, "JPEG")

設置SELinux permisions http://wiki.apache.org/httpd/13PermissionDenied看看這個,它可能有解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM