Django not displaying images which are in media folder?

55 Views Asked by At

In my django project I just want to plot some image which are there in my media folder. Eventhough the path I am seding to my frontend exists and I have verified multiple times if the path is correct. The images wont display and the error is Not Found: /home/harish/Desktop/project/visual/media/section_1_2.png [07/May/2023 16:15:52] "GET /home/harish/Desktop/project/visual/media/section_1_2.png HTTP/1.1" 404 2490

I have checked this same path with os.path.exists() and I get True as output but for some reason the images wont get displayed in the front end

SETTINGS.PY

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

    STATUSFILES_DIRS = (
    os.path.join(BASE_DIR, 'static')
    )

URLS.PY

    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', views.get_logfile_path, name = "input_log_file"),
        path('output/', views.graphs, name= 'graphs')
    ]

output/ is where I am displaying the images

VIEWS.py

    for image in sec_1_image_names:
        sec_1_image_paths.append(os.path.join(output_plots,image))
 
    # Sample outputpf sec_1_image_paths 
    # ['/home/harish/Desktop/project/visual/media/section_1_4.png']
    return render(request, 'output.html', {'image_urls':sec_1_image_paths})

OUTPUT.HTML

    <!DOCTYPE html>
    <html>
    <head>
        <title>Image View</title>
    </head>
    <body>
        <h1>Images:</h1>
        {% for url in image_urls %}
          <img src="{{ url }}" />
        {% endfor %}
    </body>
    </html>

ERROR

    "GET /home/harish/Desktop/project/visual/media/section_1_1.pn HTTP/1.1"   404 2490

0

There are 0 best solutions below