skip to Main Content

I’m using Django (Django 4.0.3.) with Apache2 to run a webserver and serving media locally.

Part of the site is a warehouse item tracker and is working fine, user can upload media (images) to items, categories etc and everything is displayed as expected.

The other part displays multiple images downloaded from an ftp server via a script I wrote in python, which downloads images, stores them in its specific folder (in the media folder) and edits the Sqlite3 db to point Django to the correct media path – these images however is not displayed when "debug=False". It works when "debug=True".

When clicking image I get the message:

Not Found

The requested resource was not found on this server.

It’s my first Django project so I’m a little out of my depth and I’m not really sure what to google.

Anyone have any ideas how to make this work or how I could do this another way?

My guess would be that it has something to do with static files?

Project structure:

|mysite
|__warehouse
|__static
|__mysite
|__media
|  |__lots of folders
|     |__(image)
|__cameras

Apache django.config:

Alias /static /home/usr/mysite/static
<Directory /home/usr/mysite/static>
    Require all granted
</Directory>

Alias /media /home/usr/mysite/media
<Directory /home/usr/mysite/media>
        Require all granted
</Directory>

<Directory /home/usr/mysite/mysite>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

Static/Media root/url:

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

2

Answers


  1. Chosen as BEST ANSWER

    I figured it out.

    My main app warehouse have url:

    http://127.0.0.1:8000/

    My secondary app have url:

    http://127.0.0.1:8000/w/

    So when I tried to display images it was trying to load from:

    http://127.0.0.1:8000/w/media/image.jpg instead of http://127.0.0.1:8000/media/image.jpg

    Added a "/" before the url in html and now it seems to be working fine.

    <img src=/{{cam.cam_media}} class="rounded img-fluid" alt="/{{cam.cam_media}}">
    

    Did I learn something from this? I'm not sure yet... but for now I'm just gonna enjoy the feeling of success!


  2. Have you tried something like:

    STATIC_ROOT = '/home/usr/mysite/static'
    STATIC_URL = 'static'
    MEDIA_ROOT = '/home/usr/mysite/media'
    MEDIA_URL = 'media'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search