django suspicious operation on image upload

2.9k views Asked by At

In my admin I try to upload an image and after clicking save I get this error:

SuspiciousOperation at /admin/the_chooser/book/add/
Attempted access to '/media/51VqHa8exoL.jpg' denied.
Request Method: POST
Request URL:    http://localhost/admin/the_chooser/book/add/
Django Version: 1.5.2
Exception Type: SuspiciousOperation
Exception Value:    
Attempted access to '/media/51VqHa8exoL.jpg' denied.
Exception Location: /Users/username/.virtualenvs/django_books/lib/python2.7/site-packages/django/core/files/storage.py in path, line 259
Python Executable:  /usr/bin/python
Python Version: 2.7.2
Python Path:    
['/Library/Python/2.7/site-packages/pip-1.3.1-py2.7.egg',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
 '/Library/Python/2.7/site-packages',
 '/Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg-info',
 '/Users/username/Projects/django_books/',
 '/Users/username/.virtualenvs/django_books/lib/python2.7/site-packages']

I just installed the Python Imaging Library to my virtualenv. This is my models.py file:

from django.db import models

LITERARY_TYPE = (
    ('F', 'Fiction'),
    ('N', 'Non-ficiton'),
)

class Book(models.Model):
    name        = models.CharField(max_length=200)
    author      = models.CharField(max_length=200,blank=True,null=True)
    slug        = models.SlugField(unique=True)
    literary_type   = models.CharField(max_length=1, choices=LITERARY_TYPE)
    description = models.TextField(blank=True)
    added       = models.DateField(blank=True,null=True,auto_now_add=True)
    cover       = models.ImageField(blank=True,null=True,upload_to='/media/')

    def __unicode__(self):
        return self.name

This is my first time working with file fields in Django. Is my upload_to parameter correct? Is something else the issue?

1

There are 1 answers

1
Joe Young On BEST ANSWER

Remove the first slash in your upload_to path so that it reads: upload_to='media/'