Search code examples
djangodjango-static

Problems Loading Static Files into Django Dev Environment


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


Solution

  • 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.