如何修复以下Django错误:“Type:IOError” “Value:[Errno 13] Permission denied” [英] How do you fix the following Django Error: "Type: IOError" "Value: [Errno 13] Permission denied"

查看:421
本文介绍了如何修复以下Django错误:“Type:IOError” “Value:[Errno 13] Permission denied”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注一个Django教程,一旦图像保存在管理员中,您就需要构建一些图像缩略图。我也使用Python的tempfile模块来保存一个临时文件名。



但是我仍然遇到以下错误:

 Type:IOErrorValue:[Errno 13] Permission denied:'c:\\\docume〜1\\myname\\\local〜1\\ \\\\somefilename'

这是我使用的代码



设置

  MEDIA_ROOT ='/ home / myname / projectname / media /' 
MEDIA_URL ='http://127.0.0.1:8000/media/'enter code here

models.py

  from string import join 
import os
from PIL import Image as PImage
从设置导入MEDIA_ROOT
from os.path import join as pjoin
from tempfile import *
from string import join
from django.db import models
from django.contrib.auth.models import来自django.contrib的用户
从django.core.files导入导入admin
文件

class Image(models.Model):
title = models.CharField(max_length = 60,blank = True,null = True)
image = models.FileField(upload_to =images /)
thumbnail = models.ImageField(upload_to =images /,blank = True,null = True)
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 save(self,* args,** kwargs):
#Save image维度
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 = F $)
tf2.close()

#小缩略图
im.thumbnail((40,40),PImage.ANTIALIAS)
thumb_fn = fn + - 大拇指+ 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 )
图像大小。
返回%sx%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 =0alt =src =/ media /%s/>< / a> %(
(self.image.name,self.thumbnail.name))
thumbnail.allow_tags =这里的Trueenter代码



ADMIN



  class ImageAdmin(admin.ModelAdmin):
#search_fields = [title]
list_display = [__unicode__,title,user,rating,size,tags _,albums_,
thumbnail创建]
list_filter = [tags,albums,user]

def save_model(self,request,obj,form,change):
obj。 user = request.user
obj.save()

我知道有更多的有效性在Django中使用图像缩略图的方法,但是我想知道为什么在缩略图时会继续获取此权限错误指甲以这种方式使用。



非常感谢所有的帮助。谢谢。

解决方案

我认为这是Windows的行为 NamedTemporaryFile 。从文档


此功能与
TemporaryFile()完全一样,除了
文件保证在文件系统中具有可见的
名称(在Unix上,
目录条目未取消链接)。可以从文件对象的名称
成员中检索
名称。 是否可以使用
名称来打开文件
第二次,而命名的临时
文件仍然是打开的,不同的
平台(可以这样用于Unix;
它不能在Windows NT或更高版本
)。


(强调我的) p>

  im.save(tf2.name, JPEG)

保存大概尝试打开该文件可以写入它。



PIL文档您可以通过保存文件对象而不是文件名,以便将

  im.save(tf2,JPEG)

可能有帮助。


I am following a Django Tutorial where you are required to construct some image thumbnails once an image is saved in admin. I am also using Python's tempfile module to save a temporary file name.

However I keep running into the following error:

"Type: IOError" "Value: [Errno 13] Permission denied: 'c:\\docume~1\\myname\\locals~1\\temp\\somefilename'"

Here is the code I am using

Settings

MEDIA_ROOT = '/home/myname/projectname/media/'
MEDIA_URL = 'http://127.0.0.1:8000/media/'enter code here

models.py

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

class Image(models.Model):
    title = models.CharField(max_length=60, blank=True, null=True)
    image = models.FileField(upload_to="images/")
    thumbnail = models.ImageField(upload_to="images/", blank=True, null=True)
    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 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" /></a>""" % (
                                                        (self.image.name, self.thumbnail.name))
thumbnail.allow_tags = Trueenter code here

ADMIN

class ImageAdmin(admin.ModelAdmin):
    # search_fields = ["title"]
    list_display = ["__unicode__", "title", "user", "rating", "size",  "tags_","albums_",
    "thumbnail", "created"]
list_filter = ["tags", "albums", "user"]

def save_model(self, request, obj, form, change):
    obj.user = request.user
    obj.save()

I know there are much more effective ways of using image thumbnails in Django however I would like to know why I keep getting this Permission error when thumbnails are used in this manner.

All help is greatly appreciated. Thanks.

解决方案

I think this is down to the behavior of NamedTemporaryFile on Windows. From the documentation:

This function operates exactly as TemporaryFile() does, except that the file is guaranteed to have a visible name in the file system (on Unix, the directory entry is not unlinked). That name can be retrieved from the name member of the file object. Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).

(emphasis mine)

in the line:

im.save(tf2.name, "JPEG")

save presumably tries to open the file so that it can write to it.

From the PIL docs you can pass save a file object instead of a filename so replacing the above with

im.save(tf2, "JPEG")

may help.

这篇关于如何修复以下Django错误:“Type:IOError” “Value:[Errno 13] Permission denied”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆