0

Trying to add media files via admin.py, they do not appear in the folder they are supposed to be. Here is my code:

settings.py

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_in_dev', 'media_root')

As far as I understand, this means that all the downloaded media files are supposed to be saved in the project_folder/static_in_dev/media_root

models.py

class GalleryCupsModel(models.Model):
    photo = models.ImageField(upload_to='cups/%Y/%m/%d')

And this is supposed to create folder 'cups' with year/month/day in the media_root folder, isn't it?

admin.py

class GalleryCupsAdmin(admin.ModelAdmin):
    list_display = ['photo']

admin.site.register(GalleryCupsModel, GalleryCupsAdmin)

urls.py

urlpatterns = [
    url(r"^cups/all/$", "gallery_cups.views.cups", name="cups"),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

project tree

├── gallery_cups
│   ├── migrations
│   │   └── __pycache__
│   └── __pycache__
├── LC
│   └── __pycache__
├── static_in_dev
│   ├── media
│   ├── media_root
│   ├── my_static
│   │   ├── css
│   │   ├── img
│   │   └── js
│   └── static_root
│       ├── admin
│       │   ├── css
│       │   ├── fonts
│       │   ├── img
│       │   │   └── gis
│       │   └── js
│       │       ├── admin
│       │       └── vendor
│       │           ├── jquery
│       │           └── xregexp
│       ├── css
│       ├── img
│       │   ├── header
│       │   └── portfolio
│       └── js
└── templates

The problem is the files, which I upload via admin.py, do not appear in the project_folder/static_in_dev/media_root, though I see them in the admin.py panel as a list of added images.

I've searched a lot but looks like I miss something crucial, but can not get what exactly. Will be thankful for any help.

Moveton
  • 253
  • 2
  • 12
  • 2
    `os.path.dirname(BASE_DIR)` actually goes up one level, so you might be storing your images outside your project root. Check that out and let us know. – Alex Morozov Feb 02 '16 at 09:02
  • Do you mean, it might create a static_in_dev/media/ folders outside of the project? I will definitely check it today (though have no chance to do this in the moment). – Moveton Feb 02 '16 at 09:06
  • Yes, that's what I mean. Let us know if that worked for you so I can make a detailed answer. – Alex Morozov Feb 02 '16 at 09:15
  • In `project_folder/static_in_dev/media_root`, `project_folder` is your project or your app folder? You also should sow the `BASE_DIR` setting. – doru Feb 02 '16 at 09:15
  • Thank you, I will definitely check it today out and let you know, if that worked. – Moveton Feb 02 '16 at 09:20
  • @AlexMorozov, thank you for the answer, it was right the needed moment. Being unintentionally during video lectures creates a looot of problems..) – Moveton Feb 02 '16 at 19:35

1 Answers1

2

From your project folder structure MEDIA_ROOT should be defined:

MEDIA_ROOT = os.path.join(BASE_DIR, 'static_in_dev', 'media_root')

because static_in_dev folder is in your project directory which is the BASE_DIR.

doru
  • 9,022
  • 2
  • 33
  • 43