Errno 13 Allowed: folder with folder with localhost

I get a message

[Errno 13] Permission denied: u '/ home / ... / ... jpg' when I try to upload a file to django. But the problem only occurs when I try to use it with localhost /. When I run or debug it using localhost: 8000, I don't get any problems.

I create function folders when I run a script to install my database.

My code is:

def handle_uploaded_file(request,f): user=Users.objects.get(id_u=request.user.id) url=settings.MEDIA_URL+'images/'+user.mail+'/gallery/'+f.name fullurl=settings.MEDIA_ROOT+'images/'+user.mail+'/gallery/'+f.name #comprobar si existe el archivo if not os.path.exists(fullurl): destination = open(fullurl, 'wb+') ... destination.close() 

Permissions in folders. I want to upload files to the "images" folder. Here I see that others do not have write permissions, but the up folder has 777. So, why does my script change permissions when creating subfolders?

 4 drwxrwxrwx 3 bernardo www-data 4096 ago 3 09:42 . 4 drwxrwxr-x 11 bernardo www-data 4096 jul 31 12:36 .. 4 drwxrwxr-x 5 bernardo bernardo 4096 ago 3 09:42 images 

My http.config

 ServerName localhost WSGIPythonPath /home/bernardo/workspace/mbosoziales Alias /media/ /home/bernardo/workspace/mbosoziales/media/ Alias /static/ /home/bernardo/workspace/mbosoziales/static/ <Directory /home/bernardo/workspace/mbosoziales/static> Order deny,allow Allow from all </Directory> <Directory /home/bernardo/workspace/mbosoziales/media> Order deny,allow Allow from all </Directory> WSGIScriptAlias / /home/bernardo/workspace/mbosoziales/mbosoziales/wsgi.py <Directory /home/bernardo/workspace/mbosoziales/mbosoziales> <Files wsgi.py> Order deny,allow Allow from all </Files> </Directory> 

So, I have to change permissions every time after running the first script, which makes folders for users, because they do not have write permissions in the media folder, although I believe that the rights to them are correct.

 sudo chgrp -R www-data images/ chmod -R o+w images/ 

Any help would be greatly appreciated. And sorry for my english :)

Added complete exception: Environment:

 Request Method: POST Request URL: localhost:8000/user/upload/ Django Version: 1.4 Python Version: 2.7.3 Installed Applications: ('django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'admin', 'login', 'suchen', 'user', 'photologue') 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/auth/decorators.py" in _wrapped_view 20. return view_func(request, *args, **kwargs) File "/home/bernardo/workspace/mbosoziales/user/views.py" in upload 332. handle_uploaded_file(request,request.FILES['file']) File "/home/bernardo/workspace/mbosoziales/user/views.py" in handle_uploaded_file 344. destination = open(fullurl, 'wb+') Exception Type: IOError at /user/upload/ Exception Value: [Errno 13] Permission denied: u'/home/bernardo/workspace /mbosoziales /media/images/ bern@ardo.com /gallery/list.csv' 
+4
source share
3 answers

When you use runserver , the process runs as a regular user; and this user has access to /home/username/ to create folders and files.

When you run it under Apache, then the code runs as an apache process - usually www-data , and this user does not have access to create folders in your home directory.

The solution is not for chmod 777 , although it works. There are two best ways to approach this:

  • For use in production, change the file path to the directory where the www-data user usually has rights; and this should not be your home directory.

  • If you must use the location in your home directory; create a new path and set the parent owner to www-data:www-data . This will allow him to work without providing the protective properties of 777 .

+7
source

Is it possible that the file you are creating here;

 destination = open(fullurl, 'wb+') 

already created and open or used somewhere when you try to execute your code?

0
source

I think not, because I used to

 if not os.path.exists(fullurl): 

therefore, opening occurs only if there is no file. I debugged it several times and it works fine, but there is a problem with permissions. At the moment, I have found a workaround that performs

 os.chmod(url,0777) 

when you first start the server and create folders.

0
source

All Articles