OSError, [Errno 13] Permission denied, thrown when I try to upload load file in django with apache/mod_wsgi server
Question: Here is the trace back

    
    
    Environment:
    
    
    Request Method: POST
    Request URL: http://mysite.com/admin/content/author/add/
    
    Django Version: 1.4.1
    Python Version: 2.7.3
    Installed Applications:
    ('grappelli',
     'filebrowser',
     'south',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'django.contrib.admin',
     'django.contrib.admindocs',
     'tinymce',
     'mailchimp',
     'content',
     'categorization',
     'mptt')
    Installed Middleware:
    ('django.middleware.common.CommonMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware')
    
    
    Traceback:
    File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
      111.                         response = callback(request, *callback_args, **callback_kwargs)
    File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in wrapper
      366.                 return self.admin_site.admin_view(view)(*args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
      91.                     response = view_func(request, *args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
      89.         response = view_func(request, *args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in inner
      196.             return view(request, *args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapper
      25.             return bound_func(*args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
      91.                     response = view_func(request, *args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in bound_func
      21.                 return func(self, *args2, **kwargs2)
    File "/usr/local/lib/python2.7/dist-packages/django/db/transaction.py" in inner
      209.                 return func(*args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in add_view
      955.                 self.save_model(request, new_object, form, False)
    File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in save_model
      709.         obj.save()
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save
      463.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base
      551.                 result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in _insert
      203.         return insert_query(self.model, objs, fields, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in insert_query
      1576.     return query.get_compiler(using=using).execute_sql(return_id)
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in execute_sql
      909.         for sql, params in self.as_sql():
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in as_sql
      872.                 for obj in self.query.objs
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py" in pre_save
      249.             file.save(file.name, file, save=False)
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py" in save
      86.         self.name = self.storage.save(name, content)
    File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py" in save
      45.         name = self._save(name, content)
    File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py" in _save
      168.                 os.makedirs(directory)
    File "/usr/lib/python2.7/os.py" in makedirs
      150.             makedirs(head, mode)
    File "/usr/lib/python2.7/os.py" in makedirs
      157.     mkdir(name, mode)
    
    Exception Type: OSError at /admin/content/author/add/
    Exception Value: [Errno 13] Permission denied: '//media/uploads'
    

Here is my model

    
    
    class ArticleImage(models.Model):
    title = models.CharField(
        verbose_name = _(u'Title'),
        help_text = _(u'The title for this article image'),
        max_length = 255,
    )
    alt_text = models.CharField(
        verbose_name = _(u'Alt. Text'),
        help_text = _(u'The alt text for this article image'),
        max_length = 255,
    )
    image = models.ImageField(
        verbose_name = _(u'Image'),
        upload_to = 'uploads/articleimages/'
    )
    article = models.ForeignKey(
        Article,
        verbose_name = _(u'Article'),
        help_text = _(u'The article to which this image belongs'),
    )
    class Meta:
        verbose_name = _(u'Image')
        verbose_name_plural = _(u'Images')
    
    def __unicode__(self):
        return u'%s'%self.title
    

Here are related settings

    
    
    MEDIA_ROOT = os.getcwd() + '/media/'
    MEDIA_URL = '/media/'
    DEBUG = False
    

Here is my httpd.conf for the media directory

    
    
    Alias /media/ /home/quinton/Sites/prod/media/
    
    <Directory /home/quinton/Sites/prod/media>
    Order deny,allow
    Allow from all
    </Directory>
    

This is not a problem when I run everything locally. Only when I push to my
server and restart Apache does this occur. I have tried changing the file
permission to 777 for the media directory, but still no luck.


Answer: `os.getcwd()` might not give you what you want. There's no guarantee that the
working directory is, say, the one your settings.py file is in.

If you want to work from your settings file's location, then you can use
`os.path` and `__file__` in your `settings.py` file:

    
    
    import os.path
    # Get the directory the settings.py file is located in.
    BASE_DIR = os.path.abspath(os.path.dirname(__file__))
    

As a bonus, other modules in your project can use this as well:

    
    
    from django.conf import settings
    print "Base directory:", settings.BASE_DIR
    

