Search code examples
djangodjango-modelsdjango-staticfilesimagefield

Django images not found in page view but was save after post request


I was trying to save an image but it can't be displayed through the browser. It was successfully saved in the exact location but when I pasted the link it says, "Page not found". Could someone help me with what's going on?

See output: Browser View

Code: Code Snippet

Settings configuration:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

STATIC_LOCATION = "static"
MEDIA_LOCATION = "media"

Solution

  • settings.py
    from pathlib import Path
    
    MEDIA_URL = '/media/'
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
    
    urls.py
    This must be applied in your settings configuration.
    from django.conf.urls.static import static
    from django.conf import settings
    
    urlpatterns = [
        ....
    ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)