[Answered ]-Django get static media files

1👍

STATIC_URL you have set doesn’t look good. I think that should be STATIC_ROOT.

You should set (for example) MEDIA_URL='/media/' and STATIC_URL='/static/' and I hope you have also enabled static file serving in urls.py

👤Rohan

1👍

Set this in settings.py:

STATIC_ROOT = '/opt/html/static/'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    '/opt/lab/labsite/static/',
)

add this in context processors:

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

and don’t forget this in INSTALLED_APPS:

  'django.contrib.staticfiles',

and then:

<img src="/static/img/logo.jpg" alt="logo" /> 

or

<img src="{{ STATIC_URL}}img/logo.jpg" alt="logo" /> 

if logo.jpg is located in /opt/lab/labsite/static/img/

In addition, STATIC_ROOT serves as a static folder to be served by Web servers/reverse proxies like Apache, Nginx, etc. If you invoke:

python manage.py collectstatic

This will copy all files from /opt/lab/labsite/static/ to STATIC_ROOT/opt/html/static/. This is useful for deployments.

But all this is for development. For production there are better ways to serve static content – https://docs.djangoproject.com/en/dev/howto/static-files/#staticfiles-production

👤Tisho

Leave a comment