Search code examples
pythondjangodjango-staticfiles

Static file images Not found Django


I'm making a website and the static files are not loading, when i try to accsess static files with localhost:8000/static/image.png it shows this: Page Not found (404)'image.png' could not be found and the index page is showing the image that displays when the image couldn't be found

files:

VoicesOnTheSpectrum
    VoicesOnTheSpectrum
        __init__.py
        asgi.py
        settings.py
        urls.py
        wsgi.py
    vots
        migrations
        static
            image.png
            logo.png
            style.css
            votstop.png
        templates
            vots
                base.html
                index.html
        __init__.py
        admin.py
        apps.py
        models.py
        tests.py
        urls.py
        views.py
    manage.py

settings.py:

from pathlib import Path
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, "vots", "templates")
SECRET_KEY = 'secret'
DEBUG = True
ALLOWED_HOSTS = []

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'VoicesOnTheSpectrum.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR,],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'VoicesOnTheSpectrum.wsgi.application'

# Static files (CSS, JavaScript, Images)
STATIC_URL =  '/static/'

STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static')]
# STATIC_ROOT = os.path.join(BASE_DIR, "static")

index:

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

{% block content %}
        <div role="img">
            <img src="{% static 'votstop.png' %}">
        </div>
{% endblock %}

base:

<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}{% endblock %}Voices On The Spectrum</title>
    <link rel="stylesheet" type="text/css" href="{% static 'style.css' %}">
</head>
<body>
    <div class="navbar">
    </div>
    <div class="body">
        {% block content %}

        {% endblock %}      
    </div>

    <div class="footer">
        <p>Copyright &copy; {% now "Y" %} Voices on the Spectrum - All Rights Reserved.</p>
        <div class="insta">
            <a href="https://www.instagram.com/voicesonthespectrum/"><img src="{% static 'image.png' %}" alt="Instagram"></a>
        </div>
    </div>
</body>
</html>

Solution

  • My Answer

    just needed to add my app to installed apps

    INSTALLED_APPS = [
    'vots',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    ]