0

I am trying to load static files into my Django project and it just isn't working.

Inside my settings.py file I have defined the below:

STATIC_URL = '/static/'
STATICFILE_DIRS = os.path.join(BASE_DIR,'static')

This is a screenshot around how my project structure is setup:

enter image description here

When the template is rendered only the text shows up - no image. HTML below to show the syntax behind static file loading:

<!doctype html>
{% load static %}
<html class="no-js" lang="">
    <head>
    </head>
    <body>
        <h1>Test</h1>
        <img src="{% static 'test.png' %}" alt="">
    </body>
</html>

Any help is really appreciated

Rutnet
  • 1,533
  • 5
  • 26
  • 48

1 Answers1

2

Make sure you run python manage.py collectstatic to populate your static folder.

Also, during development you can use the following in your main urls.py to serve your static files (from django documentation https://docs.djangoproject.com/en/2.0/howto/static-files/#serving-static-files-during-development):

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

You must have STATIC_ROOT configured in settings.py.

Eric Chiesse
  • 455
  • 3
  • 8
  • This doesn't resolve the problem. Not sure how this would, as it would just collect all the static data (for production deployment?) – Rutnet Jan 28 '18 at 01:39
  • Did you already try it? Because by default you urlconf will not point to you development static files (Did you edit your `urls.py` to allow this?). Also "production deployment" depends on you. If you say it's a folder in the same server it will be, but django will look there when trying to resolve the file locations. – Eric Chiesse Jan 28 '18 at 01:54
  • Edited to include urlconf to a serve static files in development. – Eric Chiesse Jan 28 '18 at 02:02