1

My Django site is live, as far as I know, everything works correctly except for when a user tries to upload a profile image they get this error:

OSError at /user/1/edit
[Errno 13] Permission denied: '/home/django/django_project/media/profile_pics/Square.jpg'

I have never seen this before so I'm not really sure what to do...

Here is the traceback:

Traceback Switch to copy-and-paste view

/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py in inner
            response = get_response(request) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in _legacy_get_response
            response = self._get_response(request) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in _get_response
                response = self.process_exception_by_middleware(e, request) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py in view
            return self.dispatch(request, *args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/contrib/auth/mixins.py in dispatch
        return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py in dispatch
        return handler(request, *args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/views/generic/edit.py in post
        return super(BaseUpdateView, self).post(request, *args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/views/generic/edit.py in post
            return self.form_valid(form) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/views/generic/edit.py in form_valid
        self.object = form.save() ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/forms/models.py in save
            self.instance.save() ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/db/models/base.py in save
                       force_update=force_update, update_fields=update_fields) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/db/models/base.py in save_base
            updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/db/models/base.py in _save_table
                      for f in non_pks] ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py in pre_save
            file.save(file.name, file.file, save=False) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py in save
        self.name = self.storage.save(name, content, max_length=self.field.max_length) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py in save
        return self._save(name, content) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py in _save
                    fd = os.open(full_path, flags, 0o666) ...
▶ Local vars

Update: This error also happens when user tries to upload an image in a post to the post feed, however it points to the post_pics In the media folder instead of the profile_pic Folder like it is doing in this error.

Garrett
  • 1,576
  • 4
  • 27
  • 51

2 Answers2

2

Change permission of /media directory.

sudo chmod a+rw  media
Astik Anand
  • 12,757
  • 9
  • 41
  • 51
1

You need to give your server the permission to write files into your media directory.

The simplest way is to change the media directory's group to your server's group - www-data using the chwon command.

cd into your project's directory and write this:

sudo chown -R <your-username>:www-data media

Replace <your-username> with your username, of course.

xyres
  • 20,487
  • 3
  • 56
  • 85
  • Just out of curiosity, what is the difference between your answer and `sudo chomp a+rw media` which worked and was recommended in another answer? – Garrett Sep 27 '17 at 16:41
  • @Garrett `chmod a+rw` means change the file mode for all user (`a`) to read (`r`) and write (`w`). Simply put, allow all users to read and write to the `media` directory. In my answer, I just gave the read/write permissions to `you` and the server (`www-data`). There are many more ways to change file permissions on Linux. For example, [this answer](https://askubuntu.com/a/244410/364011) adds the current user (`you`) to the server group (`www-data`) and then changes the group. The trouble with changing the perms to all users is that all the users in your system can read/write to that file. – xyres Sep 27 '17 at 16:52