django static file not loading
Asked Answered
I

11

13

I have a problem. I did everything as described in this Django tutorial (EDIT: dead link, here's a working link), and everything is running fine, but CSS and images are not showing up/being applied. How do I get the CSS and images to show up properly? Thanks for any help.

My CSS style file:

li a {
    color: red;
}
body {
    background: white url("images/background.gif") no-repeat right bottom;
}

urls.py

from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import patterns,url
from polls import views
urlpatterns = patterns('',
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
    url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
    url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'), 
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

index.html

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static '/polls/style.css' %}"/>
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
    <li><a href="{% url 'polls:detail' poll.id %}">{{ poll.question }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}

settings.py

MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    '/polls/static/'
    )
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

ROOT_URLCONF = 'mysite.urls'

WSGI_APPLICATION = 'mysite.wsgi.application'

TEMPLATE_DIRS = (
    'C:/django poll project/mysite/templates',
    )

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

On runserver command, getting:

[18/Sep/2014 17:40:51] "GET /polls/ HTTP/1.1" 200 311
[18/Sep/2014 18:25:39] "GET /polls/ HTTP/1.1" 200 311
Irrespective answered 18/9, 2014 at 13:20 Comment(3)
Where did you put your CSS file?Harvestman
\django poll project\polls\static\polls\style.cssIrrespective
Implement this code inside your project: Visit this link: <#66133233>Urbannal
P
12

The URL to the static-Files is "yourDomain/static/"

When you want to access to your "style.css" you should use "/static/style.css" instead of "/polls/style.css"

EDIT:

Change this part of your settings.py

STATICFILES_DIRS = (
    '/polls/static/'
    )

to

STATICFILES_DIRS = (
    'C:/django poll project/mysite/static'
    )

better would be:

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

Then the folder is called "static" and is on the same level where the "manage.py" is. When you put your style.css in this "static"-folder you can call it with "/static/style.css"

Platitude answered 18/9, 2014 at 13:25 Comment(2)
Doesn't reflect any changesIrrespective
Worked for me like this: STATICFILES_DIRS = ('/projectpath/static',).Iover
M
17

Updated Answer For Django 2.2 - 2019

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

BASE_DIR is already defined in settings.py

Also when loading, do {% load static %} instead of {% load staticfiles %}

Monochasium answered 2/5, 2019 at 21:11 Comment(2)
NOTICE: it's a tuple. Don't forget the comma at the end.Monochasium
NOTICE: It can also be a list. like ``` STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] ```Monochasium
P
12

The URL to the static-Files is "yourDomain/static/"

When you want to access to your "style.css" you should use "/static/style.css" instead of "/polls/style.css"

EDIT:

Change this part of your settings.py

STATICFILES_DIRS = (
    '/polls/static/'
    )

to

STATICFILES_DIRS = (
    'C:/django poll project/mysite/static'
    )

better would be:

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

Then the folder is called "static" and is on the same level where the "manage.py" is. When you put your style.css in this "static"-folder you can call it with "/static/style.css"

Platitude answered 18/9, 2014 at 13:25 Comment(2)
Doesn't reflect any changesIrrespective
Worked for me like this: STATICFILES_DIRS = ('/projectpath/static',).Iover
T
5

for Django version 4 2022

if anyone's static file is not working,make sure your static folder is in the right location

shortcut: Keep your static folder in the same directory where your database is located.

-->ProjectName
  -->app1
  -->app2
  -->db.sqlite3
  -->static

enter image description here

and make sure you have added this lines in settings.py

STATIC_URL = 'static/'
STATICFILES_DIRS = (
BASE_DIR/'static',
)
Transparent answered 30/5, 2022 at 9:32 Comment(1)
I am getting "The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting." error message. It just doesn't want to pull the static files, always gives "not found" error for all my static files.Kamerman
M
2

It worked for me :

PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, '..', 'static'),
)
Meave answered 2/1, 2018 at 13:4 Comment(0)
O
2

If problem bad variable {{STATIC_URL}} in template files, add

'django.template.context_processors.static'

to this part of settings.py module:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': TEMPLATE_DIRS,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.template.context_processors.static',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
Organzine answered 9/1, 2021 at 1:46 Comment(0)
G
2

Following the documentation: https://docs.djangoproject.com/en/3.2/howto/static-files/

And restarting the server worked for me

Goodill answered 21/10, 2021 at 17:19 Comment(0)
K
2
  1. Remove django.contrib.staticfiles from INSTALLED_APPS.
  2. Set STATIC_ROOT to the drive folder with the static files.
  3. Set STATIC_URL to the browser folder/prefix that will point at the files.

The problem is STATIC_ROOT and STATICFILES_DIRS cannot be in the same folder and try to act differently...

STATICFILES_DIRS

  • exists because different installable apps have different static resources.

  • Through django magic, in development these files are served from each app.

  • This allows editing assets and re-running the server without worrying about the STATIC_ROOT cache.

  • Also requires a collector that assembles static files into STATIC_ROOT.

STATIC_ROOT

  • The place the files are served from in production.
  • A simple browser path to folder mapping.
  • Not recommended for production because usually NGINX or AppEngine handles this.
  • Works because you are skipping the collector and telling Django to look here always.
  • Will not work if an app has its own assets, you will need to use STATIC_DIRS.
Khufu answered 10/11, 2021 at 21:43 Comment(0)
B
1
STATIC_URL = '/static/'
STATICFILES_DIRS = (
   os.path.join(BASE_DIR, 'static'),
)

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'images')
Baileybailie answered 18/3, 2020 at 20:54 Comment(0)
T
0

Currently works on Django Version 3.x

{% load static %}
<!DOCTYPE html>
<html>
    <head>
        <title>Website</title>
        <link rel="stylesheet" href="{% static 'css/style.css' %}">
    </head>
    <body>
        <h1>Hello user!</h1>
        <p>something you want</p>
    </body>
</html>

To work above Implementation Following code must be added in project setting file.

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

Here static is a directory in project root.

Tullus answered 28/8, 2020 at 10:49 Comment(0)
T
0

For the newer version of Django use the following command. It solved my issues.

STATIC_URL = 'static/'

STATICFILES_DIRS = [ BASE_DIR / "static", '/var/www/static/', ]

{% load static %}

I followed this documentation (https://docs.djangoproject.com/en/4.1/howto/static-files/)

Tritium answered 7/4, 2023 at 13:43 Comment(0)
O
-1

When you run 'python manage.py runserver' add any post number like 2000 or whatever it will look like 'python manage.py runserver 2000' it will solve the problem

Oilskin answered 21/4, 2022 at 6:17 Comment(2)
Well, it is used for changing port only, and we can also change IP, this is not an answer, with high respect I am saying to you :).Infracostal
totally irrelevant answer to the question askedMadalynmadam

© 2022 - 2024 — McMap. All rights reserved.