views:

354

answers:

1

Hello, I am working on a django app. One part would involve uploading files (e.g. spreadsheet or whatever). I am getting this error:

IOError at /fileupload/

[Errno 13] Permission denied: 'fyi.xml'

Where 'fileupload' was the django app name and 'fyi.xml' was the test document I was uploading.

So, I used chmod and chown to make the [project directory]/static/documents/ folder writable to apache. Actually I even tried just making it chmod 777, still no luck.

So, in my settings.py I just changed where my MEDIA_ROOT was:

MEDIA_ROOT = '/var/www/static/'

Then, in case it was an SELinux thing, I created the new documents directory in /var/www/static'...

drwxr-xr-x 2 apache root 4096 Aug 13 11:20 documents

Then I did these commands to try to change the context so apache would be allowed to write here. I'm not too familiar with this distro, it's the flavor of Red Hat we're given, so I've never had to go beyond chmod and/or chown to fix a permissions problem.

sudo chcon -h system_u:object_r:httpd_sys_content_t /var/www/static
sudo chcon -R -h root:object_r:httpd_sys_content_t /var/www/static
sudo chcon -R -h root:object_r:httpd_sys_content_t /var/www/static/*

None of this made any difference. To be honest, I'm not positive that I even have SELinux here but since normal unix permissions didn't seem to work I thought I'd try it.

So, does anyone have an idea on what to look at next? Not sure how much code I should post here, but in case it would be helpful here's what's in my views.py:

views.py
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from forms import UploadFileForm
from fyi.models import Materials

def handle_uploaded_file(f):
    destination = open('fyi.xml', 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()


def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['document'])
            form.save()
            template = 'upload_success.html'
    else:
        form = UploadFileForm()
        template = 'fileupload.html'
    return render_to_response( template, {'form': form})

...any help would be appreciated.

A: 

Maybe try changing:

destination = open('fyi.xml', 'wb+')

to something like:

upload_dir = settings.MEDIA_ROOT # or wherever
destination = open(os.path.join(upload_dir, 'fyi.xml'), 'wb+')

If it is an SELinux issue, perhaps this page would help:

ars
That worked, thanks!Just FYI for others, I also needed of course to add:import osfrom django.conf import settings...to the top of views.py.
rossdavidh
You're welcome! Glad it worked out. :)
ars