0

I am trying to serve my Django project, set up with django-skel 1.4, using the development server. My site runs as expected except for my images, they are not served.

Part of templates/home.html

<img width="65px;" src="assets/img/pic.png" alt="" id="symbol" />

I'm guessing I should change something in this part: src="assets/img/pic.png". I've looked around in SO threads and tweaked according to the given answers but I could not manage to make it work.

So how do I properly set images in templates?

Other relevant information:

settings.common.py

DJANGO_ROOT = dirname(dirname(abspath(__file__)))

MEDIA_ROOT = normpath(join(DJANGO_ROOT, 'media'))
MEDIA_URL = '/media/'

STATIC_ROOT = normpath(join(DJANGO_ROOT, 'static'))
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    normpath(join(DJANGO_ROOT, 'assets')),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'compressor.finders.CompressorFinder',
)

Tree of project

.
├── apps
│   └── __init__.py
├── assets
│   ├── css
│   │   └── base.css
│   ├── img
│   │   └── pic.png
│   └── js
├── default.db
├── __init__.py
├── libs
│   ├── core
│   │   ├── admin.py
│   │   ├── __init__.py
│   │   ├── models.py
│   │   ├── views.py
│   │   └── views.pyc
│   └── __init__.py
├── settings
│   ├── common.py
│   ├── dev.py
│   ├── __init__.py
│   └── prod.py
├── templates
│   ├── 404.html
│   ├── 500.html
│   ├── home.html
│   └── install.html
└── urls.py

Btw: Please no solutions using if settings.DEBUG, preferably if possible without needing to adapt urls.py.

Edit

Tree of the top level directory after doing collectstatic

.
├── fabfile.py
├── gunicorn.py.ini
├── manage.py
├── Procfile
├── project_name
│   ├── apps
│   │   └── __init__.py
│   ├── assets
│   │   ├── css
│   │   │   └── base.css
│   │   ├── img
│   │   │   └── pic.png
│   │   └── js
│   ├── default.db
│   ├── __init__.py
│   ├── libs
│   │   ├── core
│   │   │   ├── admin.py
│   │   │   ├── __init__.py
│   │   │   ├── models.py
│   │   │   └── views.py
│   │   └── __init__.py
│   ├── settings
│   │   ├── common.py
│   │   ├── dev.py
│   │   ├── __init__.py
│   │   └── prod.py
│   ├── static
│   │   │   └── js
│   │   ├── css
│   │   │   └── base.css
│   │   └── img
│   │       └── pic.png
│   ├── templates
│   │   ├── 404.html
│   │   ├── 500.html
│   │   ├── home.html
│   │   └── install.html
│   └── urls.py
├── README.md
├── reqs
│   ├── common.txt
│   ├── dev.txt
│   └── prod.txt
├── requirements.txt
└── wsgi.py

Edit 2

My understanding how Django reads the path: Let src="static/img/pic.png", from my settings.common.py:

>>> DJANGO_ROOT
'/home/my_username/web/my_project/my_project'
>>> j = os.path.join(DJANGO_ROOT, 'static/')
>>> print j
/home/my_username/web/my_project/my_project/static

But

>>> STATIC_URL
'/static/'
>>> j = os.path.join(DJANGO_ROOT, STATIC_URL)
>>> print j
/static/

So somewhere Django probably does os.path.join that is the only reason I can think of why src="static/img/pic.png" works but src="{{STATIC_URL}}img/pic.png" doesn't. But why then does this apparently work for other people but not for me?

Bentley4
  • 10,678
  • 25
  • 83
  • 134

2 Answers2

0

I think you have to add the following to urls.py

(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': settings.MEDIA_ROOT,
    }),

at least thats how I always do it...

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

You need to add

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'django.core.context_processors.static',
)

and add staticfiles to your INSTALLED_APPS

You need to modify your img tag

<img width="65px;" src="{{STATIC_URL}}assets/img/pic.png" alt="" id="symbol" />

If it is user uploaded content then replace {{STATIC_URL}} with {{MEDIA_URL}}

Also see related Django cannot find my media files (on development server)

Community
  • 1
  • 1
Pratik Mandrekar
  • 9,362
  • 4
  • 45
  • 65
  • I tried `{{STATIC_URL}}` as well as `{{MEDIA_URL}}` but this did not work. I don't understand how that could work as my `STATIC_URL` is `/static/` and `MEDIA_URL` is `/media/` (See question post for the settings). Btw it is for use in the development server. – Bentley4 Sep 12 '12 at 16:03
  • Did you try `src="{{STATIC_URL}}assets/img/pic.png"` ? – Pratik Mandrekar Sep 12 '12 at 18:31
  • 1
    Besides you should also run ./manage.py collectstatic in your project root – Pratik Mandrekar Sep 12 '12 at 18:33
  • I tried `src="{{STATIC_URL}}assets/img/pic.png"` as well as `src="{{MEDIA_URL}}assets/img/pic.png"`. I also ran `collectstatic`. Still does not serve my images. – Bentley4 Sep 12 '12 at 20:03
  • Do you have any other clue what is going wrong? I would be VERY grateful! Could you show a tree of a sample project? (Btw the program `tree` is handy for printing file trees) – Bentley4 Sep 12 '12 at 22:46
  • 1
    Can you access the static assets by browsing to `localhost:8000/static/assets/img/pic.png` ? In `DEBUG=True` mode it should show you some error. Can you paste that? – Pratik Mandrekar Sep 13 '12 at 06:55
  • `Request URL: http://localhost:8000/static/assets/img/pic.png` returns `'assets/img/pic.png' could not be found` . I updated the original post so you can see my tree after I ran `collectstatic` and from the top level directory. – Bentley4 Sep 13 '12 at 09:42
  • While joining do not use the trailing `/` and secondly use `{{STATIC_URL}}`, your code shows only one `{`. Below works fine for me and I put my assets under `project_static` and then run collectstatic which pulls the files to `STATIC_ROOT`. DIRNAME = os.path.dirname(__file__) STATIC_URL = "/static/" STATIC_ROOT = os.path.join(DIRNAME ,'static') STATICFILES_DIRS = ( os.path.join(DIRNAME , 'project_static'), ) – Pratik Mandrekar Sep 13 '12 at 11:20
  • Sorry about the single `{}` in my post, I did it correct in my project. Still does not work. I then changed `STATIC_ROOT` to `STATIC_ROOT = "static/"`. This also did not work, so that looks like the template syntax is not working correctly. But I also use `href="{% load static %}{% get_static_prefix %}css/base.css"` in my html file and this works perfectly... . So I still don't understand why using `{{STATIC_URL}}` does not work. – Bentley4 Sep 13 '12 at 11:40
  • If {{ STATIC_URL }} isn't working in your template, you're probably not using RequestContext when rendering the template. You can try figuring out from here, I think thats why `get_static_prefix` is working. https://docs.djangoproject.com/en/1.3/howto/static-files/#with-a-template-tag – Pratik Mandrekar Sep 13 '12 at 12:13