'Django Admin Page not found when including MEDIA_ROOT setting
this is my first question so please point me to the right direction in case I miss something of importance ☺️
So my problem is the following:
I'm currently creating a website with Django, which is supposed to list a variety of projects from the database. Each project has a picture associated with it. The path to this picture is saved in the database (each picture is added as models.ImageField()
to the projects model). In order to serve them, I added MEDIA_ROOT = os.path.join(BASE_DIR, 'main/media')
to my settings.py
and added the following snippet to my urlpatterns
in urls.py
: + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Now to the crux of it: Everything works fine, except I can't open my admin panel now. The media files are served as expected in the projects overview, but at soon as I try to open the /admin page, I get following error message:
Page not found (404)
“/Users/.../main/media/admin” does not exist
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Raised by: django.views.static.serve
as soon as I remove + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from my url patterns, the admin page works fine again, however the media files are not served anymore.
Can someone show me what I've broken here? Thank you very much!
Solution 1:[1]
I had the same problem. I had a link on my site, that when pressed took me to admin panel.
The mistake was that I was missing a trailing slash.
This did not work:
<a href="/admin">Admin</a>
This worked:
<a href="/admin/">Admin</a>
Solution 2:[2]
You might not have added the MEDIA_URL path in setting.py. In your case:- add this to settings.py:- MEDIA_URL = "/main/media/". If it doesn't work, try removing one of the two corner slashes. Hope it will work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Henrik |
Solution 2 | arpit abhyankar |