0

I have a Django 3.0 Model with a FileField:

# models.py
class Profile(models.Model):
    ...
    photo = models.FileField(
        upload_to='photos/',
        help_text='Select photo to upload'
    )

My settings.py file defines MEDIA_ROOT in terms of BASE_DIR as

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'myApp/static/myApp/')

and I've confirmed that

MEDIA_ROOT = ~me/ProjectRoot/myProject/myApp/static/myApp/

exactly as I intend.

When I upload a photo file through the Profile Admin "add" page, that photo appears in the folder ~me/ProjectRoot/myProject/myApp/static/myApp/photos/ exactly as intended. When I revisit that Profile's Admin "change" page, the upload widget displays as

Currently: photos/uploaded_photo.png     [ ] clear
Change: [Browse]   No file selected.

showing a link that's ostensibly to the file I've already uploaded. The link itself is to

http://127.0.0.1:8000/admin/myApp/profiles/9/change/photos/uploaded_photo.png

(here, '9' is the primary key id of the Profile in question in the database).

When I select the link, instead of viewing the uploaded photo, the Admin home page loads with a warning banner at the top that says "Profile with ID “9/change/photos/uploaded_photo.png” doesn’t exist. Perhaps it was deleted?"

If I go to the database itself, I can clearly see that the Profile exists there:

mysql> SELECT * FROM myApp_profile;

+----+----------------------------------+
| id | photo                            |
+----+----------------------------------+
...
| 9  | photos/uploaded_photo.png        |

I also double-checked the upload folder, and the file myApp/static/myApp/photos/uploaded_photo.png is still there, undeleted.

I've read the following Documentation pages, which did not address this issue:

https://docs.djangoproject.com/en/3.0/topics/http/file-uploads/

https://docs.djangoproject.com/en/3.0/ref/contrib/admin/

https://docs.djangoproject.com/en/3.0/ref/models/fields/#filefield

https://docs.djangoproject.com/en/3.0/topics/files/

I've also found this Stack Overflow question. All of its answers involve MEDIA_ROOT not being set correctly, which I don't believe is my problem.

What's wrong and how do I fix it?

Darien Marks
  • 446
  • 4
  • 18
  • How do you normally access your static files? Can you share your project `urls.py`? – Bernardo Duarte Jun 13 '20 at 14:08
  • @BernardoDuarte I normally access static files using `{% load static %}` and `{% static 'myApp/filepath/file.ext' %}` in the template. My `urls.py` contains only `url_patterns` with `path('admin/', admin.site.urls),` plus URL config for non-admin views that aren't related to the problem. – Darien Marks Jun 13 '20 at 14:14
  • Can you have a look at [this docs](https://docs.djangoproject.com/en/3.0/howto/static-files/) I believe that will help you, however I'm not sure whats causing your problem. – Bernardo Duarte Jun 13 '20 at 14:20
  • @BernardoDuarte Thanks, but I'm already using static files successfully everywhere else in the app. This is a problem specifically with the Admin, and there's nothing in that link that applies – Darien Marks Jun 13 '20 at 14:27
  • I find [this one](https://stackoverflow.com/q/18632566/7395911) very similar, can you have a look? – Bernardo Duarte Jun 13 '20 at 14:52
  • Thanks for another lead. Unfortunately, the only good answer there is out-of-date and just doesn't work with Django 3 (url() in `urls.py` no longer exists, for example). Since the poster there doesn't explain what they're doing, I can't adapt it to Django 3. – Darien Marks Jun 13 '20 at 17:21
  • you need to add photos folder in Media_root 'myApp/static/myApp/photos' causer the location shows that the image is inside photo folder – ngawang13 Jun 15 '20 at 13:43

0 Answers0