Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Error (13, 'Permission denied')

I'm been working on this Photo Organizer and Sharing App Part I at http://lightbird.net/dbe/photo.html. I'm trying to generate a thumbnail and when I do . I get this error.

I have 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

My models.py is

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()

The problem is here:

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)

How do I fix this error?

like image 566
supersheep1 Avatar asked Feb 10 '13 13:02

supersheep1


People also ask

How do I fix error 13 Permission denied?

We can solve this error by Providing the right permissions to the file using chown or chmod commands and also ensuring Python is running in the elevated mode permission .

What is Permission denied 13?

Error 13 indicates a filesystem permissions problem. That is, Apache was denied access to a file or directory due to incorrect permissions. It does not, in general, imply a problem in the Apache configuration files.

What is error number 13 in Python?

The PermissionError: [errno 13] permission denied error occurs when you try to access a file from Python without having the necessary permissions. To fix this error, use the chmod or chown command to change the permissions of the file so that the right user and/or group can access the file.

Why is my permission denied in Python?

Permission denied simply means the system is not having permission to write the file to that folder. Give permissions to the folder using "sudo chmod 777 " from terminal and try to run it.


1 Answers

It looks to me like django doesn't have the permissions it needs to access your MEDIA_ROOT folder.

Have a look at your MEDIA_ROOT settings in your settings.py file. Then check the permissions on the folder (something like ls -lsa /path/to/media_root from a bash shell). Make sure the user running django as write permission to the folder.

Also, as asermax points out, make sure you have created an images directory within your MEDIA_ROOT.

Have a look at the documentation for serving static files particularly the section on serving other directories

UPDATE

Perhaps it's this issue. Try replacing im.save(tf2.name, "JPEG") with im.save(tf2, "JPEG")

like image 113
Aidan Ewen Avatar answered Oct 25 '22 14:10

Aidan Ewen