Can't find image files

485 views
Skip to first unread message

Dick Arnold

unread,
Jan 27, 2020, 7:52:06 PM1/27/20
to Django users
My nice, new Django application is going along fairly well,  although I have run into a couple of roadblocks and need some help. The first problem is my HTML <img statement cannot find a jpg file.  Here's the HTML statement:

<img src=GAW.jpg alt="GAW.jpg">

Here's the error message I get on my terminal (command prompt) window:

Not Found: /GAW.jpg
[25/Jan/2020 11:51:50] "GET /GAW.jpg HTTP/1.1" 404 3481

I read every thing I could find and most of them tell me that it is in a location relative to the "current folder", but I can find nothing that explains what is meant  by "current".  I thought it might be where my terminal prompt comes from.   It's in the highest level project folder. Tried that and still no luck.

Antje Kazimiers

unread,
Jan 27, 2020, 10:51:56 PM1/27/20
to django...@googlegroups.com

Hi,

I keep my images files within in a static folder, so

./MyProject/static/media/myimage.png

would be my path. In the template I make use of it like that:

<img src="{% static 'media/myimage.png' %} alt="my image">

It's explained here:

https://docs.djangoproject.com/en/3.0/howto/static-files/

also that you need to define your static folder in your settings file:
STATIC_URL = '/static/'

and that you might need to run collectstatic first, if your DEBUG variable is set to false in your settings file.


Kind regards,
Antje

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4dd2997f-0063-4300-b075-404f99569f8e%40googlegroups.com.

Dick Arnold

unread,
Jan 30, 2020, 11:45:51 AM1/30/20
to Django users
'about a day or so after my original post I discovered that static files might be related to my problem..  getting the first reply that pointed me in the same direction gave my hope that a solution was close.  unfortunately, nothing worked.  Below I have the pertinent code.  hopefully, someone can spot something I'm doing that that is incorrect.

In my settings files, I have:
INSTALLED_APPS = [
    # My Apps
    'contacts',
    # Default django apps.
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

DEBUG = True
STATIC_URL = '/static/'

Here's my html code:

{% load static %}
<img src="{% static 'GAW.jpg' %}" alt="GAW.jpg">

my directory structure is:
project older
     project folder (same name as higher folder)
          application folder\

I created a static folder and put my image file in it.  I put a copy of this folder in all 3 folders in my directories.

I tried some variations on the html code.  
    put a forward slash before the image file name.
    put two dots in front of the slash.
   tried each of these with a slash after the image file name
   ran all 4 of thee with and without quote marks around the image file name.

all these variations got almost identical error messages...
[30/Jan/2020 10:42:13] "GET /static/GAW.jpg HTTP/1.1" 404 1647
some of them ended in 1650 instead of 1647
I'm not getting the message any more
Not Found: /GAW.jpg

Can anyone see anything I'm doing wrong?

Dick Arnold

unread,
Jan 30, 2020, 11:55:42 AM1/30/20
to Django users
Please ignore. I finally got it to work,  It was the directory structure to the image file.  Needed: 

project folder
    project folder
        application folder
            'static' folder
                application folder
                     image file
application

On Monday, January 27, 2020 at 6:52:06 PM UTC-6, Dick Arnold wrote:

Dick Arnold

unread,
Jan 31, 2020, 10:09:23 AM1/31/20
to Django users
Didn't stay working for long.  Needed one more change.

the src also needs the application folder before the image file name.

<img src="{% static 'contacts/GAW.jpg' %}" alt="GAW.jpg" >  (contacts is the name of my applications folder.)

On Monday, January 27, 2020 at 6:52:06 PM UTC-6, Dick Arnold wrote:

maninder singh Kumar

unread,
Jan 31, 2020, 10:00:22 PM1/31/20
to Django users
When you load files to static, it loads to a common static location.  That is the way you would want it to be.  Recommended read django docs.

regs

usama Naseer

unread,
Feb 1, 2020, 8:42:56 AM2/1/20
to django...@googlegroups.com
--
Reply all
Reply to author
Forward
0 new messages