[Fixed]-How to point correctly to static image in django


Static files can be confusing in Django. I’ll try to explain as simply as possible…

This is the directory that you should serve static files from in production.

This is the directory that you should serve static files from in development.

STATIC_ROOT and STATICFILES_DIRS cannot point to the same directory.

Django is a highly modular framework. Some application modules contain their own templates, css, images and JavaScript. Django admin is one such app. Django extends this modularity to applications you create by using different directories for static files in development versus production.

When DEBUG = True and you have included django.core.staticfiles in your INSTALLED_APPS, Django will serve the files located in the STATICFILES_DIRS tuple using the STATIC_URL path as the starting point.

In production this responsibility should be given to Nginx, Apache, CloudFront, etc. When DEBUG = False, Django will not automatically serve any static files.

When you run:

$ python manage.py collectstatic

The files specified in the STATICFILES_DIRS are copied into the STATIC_ROOT to be deployed.

So, to answer your question, I would do the following:

Create another directory to store your static files in development and add that path to your STATICFILES_DIRS. I usually call this folder “static-assets”. It can reside at the same level as your existing “static” directory.

Set STATIC_ROOT to the path to your existing “static” directory.

If you look closely at the path that’s returning a 404 in your screenshot, the image path is specified as: /static/img/logo.png, but your directory for images is: /static/image/

So double-check your image path to make sure you’re pointing to the right directory.


Make folder “staticfiles” in root of your project where settings.py exists

In staticfiles you can go this way..

  • css
  • js
  • images

In settings.py

STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'

                os.path.join(PROJECT_DIR,'staticfiles'), # if your static files folder is named "staticfiles"
                os.path.join(PROJECT_DIR,'template'), # if your static files folder is named "template"

In base.html

<link rel="stylesheet" type="text/css" href="{% static 'css/demo.css' %}" /> 

<script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>

In other template files in which you include base.html

{% extends "base.html" %}
{% load static %}

<script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>

<div id="yourID" class="yourClass">
    <img src="{% static "images/something.gif" %}" alt="something" >


If you are in development mode then define just one line:

STATICFILES_DIRS = ( os.path.join('static'), )

You don’t need to define STATIC_ROOT = ‘/Users/myuser/myprojectname/myprojectname’



So, working solution is :

STATIC_URL = '/static/'

STATICFILES_DIRS = ( os.path.join('static'), )


actually I too was struggling with the same problem , it got resolved now , check javapoint documentation once its really good , another method that worked for me is change the path of static folder i.e place the static folder inside the app folder that u have created (in my case , the static folder is inside the first_app folder) . it should work . after that I relocated my folders back to where it was and to my surprise , it worked again!. sometimes it may be the fault of browser . I tried my level best to explain as I am a beginner in Django .


If you want to save static files in Django you will need to do the following

in settings.py file you will have something like this `

STATIC_URL = '/static/'
    os.path.join(BASE_DIR, 'my_project/static')  # where I want to host the static files i.e img, js & css

depends on how you have structure you files but you will need to have something like this in your urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
    path('', include('appfront.urls')),
    path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Final your template will have something like this Note the usage of static tag

{% load static %}
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-dark bg-primary sticky-top">
    <div class="container">
      <a class="navbar-brand" href="{% url 'index' %}">
        <img src="{% static 'img/logo.png' %}" class="logo" alt="">

Leave a comment