Samuel Muiruri
Samuel Muiruri

Reputation: 522

Django Serving Static Files not loading

Setting up a new django application with the following settings:

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

DEBUG = True

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

STATIC_URL = '/static/'

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'kb/media')
STATIC_ROOT = os.path.join(BASE_DIR, 'kb/static/')

Setup a template

{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
    <title>HomePage</title>
</head>
<body>
    <img src="{% static 'img/girl.png' %}">
    <img src="/media/boy-512.png">
</body>
</html>

I assume there's an issue with the settings of how it's supposed to get the static files. In this case the directory for the static image girl.png is kb\kb\static\img\girl.png

Upvotes: 1

Views: 3697

Answers (2)

Neeraj Vishwakarma
Neeraj Vishwakarma

Reputation: 122

add your project name in settings.py

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

Upvotes: 0

Essex
Essex

Reputation: 6118

If I can help you, this is my settings.py file and one example in my html template :

I have a project : MyProject and one application : my_project (inside I have static document > images documents > test.png file)

My settings.py file looks like :

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "my_project/static"),)

And my HTML template file with image looks like :

<img src="{% static 'images/test.png' %}" />

Hopfully to help you

Upvotes: 3

Related Questions