Search code examples
djangodjango-staticfiles

django static file not loading


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

Solution

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