# Save photos outside django folder in production

**URL:** <https://forum.djangoproject.com/t/save-photos-outside-django-folder-in-production/18739>\
**Category:** Forms & APIs\
**Created:** [February 9, 2023, 3:44pm UTC](https://forum.djangoproject.com/t/save-photos-outside-django-folder-in-production/18739 "2023-02-09T15:44:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pietro-Putelli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/pietro-putelli/32/12129_2.png) [@Pietro-Putelli](https://forum.djangoproject.com/u/Pietro-Putelli)\
**Post date:** [February 9, 2023, 3:44pm UTC](https://forum.djangoproject.com/t/save-photos-outside-django-folder-in-production/18739/1 "2023-02-09T15:44:47Z")

</div>

Hi, here’s my code but it doesn’t work:

```auto
def path_and_rename(instance, filename):
ext = filename.split(‘.’)[-1]
# get filename
if instance.pk:
filename = ‘{}.{}’.format(instance.pk, ext)
else:
# set filename as random string
filename = ‘{}.{}’.format(uuid4().hex, ext)
# return the whole path to the file
return os.path.join(‘account/’, filename)

class Profile(models.Model):
picture = models.FileField(upload_to=path_and_rename)

# settings.py

DEBUG = False
MEDIA_ROOT = “/Users/myuser/Desktop/django-sample/”
MEDIA_URL = “account/”

```

Once I try to upload a photo using django admin terminal the source is not uploaded anywhere.  
I know this is not a good approach in production mode, but it’s the start of my project and I don’t want to spend money for Amazon S3 o similar services for now.

Does someone have a solution?  
Thank you

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 9, 2023, 3:54pm UTC](https://forum.djangoproject.com/t/save-photos-outside-django-folder-in-production/18739/2 "2023-02-09T15:54:34Z")

</div>

First, this code is not properly indented and will not run as posted here. Also, where does this function reside?

How are you running this? Is this being done in your local development environment? Are you running this using `runserver`? (If not, how?)

There should be some error messages generated by your server.

You could add a print statement in that function to show you what you’re getting as the instance and filename parameters, along with the value of the filename that you’re generating.

---

<div class="post-metadata">

**Author:** ![Pietro-Putelli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/pietro-putelli/32/12129_2.png) [@Pietro-Putelli](https://forum.djangoproject.com/u/Pietro-Putelli)\
**Post date:** [February 9, 2023, 6:56pm UTC](https://forum.djangoproject.com/t/save-photos-outside-django-folder-in-production/18739/3 "2023-02-09T18:56:45Z")

</div>

I’m running this code on my local machine using docker-compose and I don’t get any error from the server.

Here’s how I handle error logs:

```auto
LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "verbose": {
            "format": "%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s"
        },
        "simple": {"format": "%(levelname)s %(message)s"},
    },
    "handlers": {
        "file": {
            "level": "WARNING",
            "class": "logging.FileHandler",
            "filename": "errors.log",
            "formatter": "verbose",
        },
    },
    "loggers": {
        "errors": {
            "handlers": ["file"],
            "propagate": True,
            "level": "WARNING",
        },
    },
}

```

And after saving the file, there are no erorrs.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 9, 2023, 7:30pm UTC](https://forum.djangoproject.com/t/save-photos-outside-django-folder-in-production/18739/4 "2023-02-09T19:30:14Z")

</div>

Is that MEDIA\_ROOT directory inside the container or is it a volume mount to a physical directory outside the container? If outside, what’s the mount command look like for it?

---

<div class="post-metadata">

**Author:** ![Pietro-Putelli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/pietro-putelli/32/12129_2.png) [@Pietro-Putelli](https://forum.djangoproject.com/u/Pietro-Putelli)\
**Post date:** [February 9, 2023, 10:09pm UTC](https://forum.djangoproject.com/t/save-photos-outside-django-folder-in-production/18739/5 "2023-02-09T22:09:25Z")

</div>

I tried to map the folder as you said, and now it works, thank you.  
Here’s my docker-compose configuration.

```auto
  app:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
      - ../photos/:/code/media
    ports:
      - 8000:8000
    depends_on:
      - database

```
