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?