Search code examples
pythondjangostylesheetstatic-files

Django stylesheet will not load


I have read through the limitless threads on static files and all the issues that seem to occur, but nothing fixes my issue. I spent a lot of time getting my "media" folder working yesterday, but I cannot seem to get the static files working.

My template looks in the correct location for the file, and the file is there, but I get a 404 when the template attempts to load it.

Here is my settings.py:

STATIC_PATH = os.path.join(BASE_DIR, 'static')
STATICFILES_FINDERS = ( 
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

MEDIA_URL = '/media/' 
MEDIA_ROOT = 'public/media/' 
STATIC_URL = '/static/' 
# STATIC_ROOT = 'public/static/'
STATIC_DIRS = (
    STATIC_PATH,
)

base.html

<!DOCTYPE html>

<html>
    <head lang="en">
        <meta charset="UTF-8">
        <title>Address Book</title>
        <link rel="stylesheet" type="text/css" href="{{STATIC_URL}}address_book/stylesheets/1.css"
    </head>
        {% if user.is_authenticated %}
        <h1>Hello {{ user.username }}!</h1>
        {% else %}
        <h1>Please login below</h1>
        {% endif %}
    <body>
        {% block body_block %}{% endblock %}
    </body>

    <h2> Need to make changes? </h2>
    <ul>
        <li><a href="{% url 'index' %}">Go home</a></li>
        {% if user.is_authenticated %}
        <li><a href="{% url 'logout' %}">Logout</a></li>
        <li><a href="{% url 'add_client' %}">Add new client</a></li>
        {% else %}
        <li><a href="{% url 'login' %}">Login</a></li>      
        <li><a href="{% url 'register' %}">Register here</a></li>  
        {% endif %}
    </ul>

urls.py

from django.conf.urls import patterns, url
from address_book import views
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = patterns('',
        url(r'^$', views.index, name='index'),
        url(r'^add_client/$', views.add_client, name='add_client'),
        url(r'^register/$', views.register, name = 'register'),
        url(r'^login/$', views.user_login, name='login'),
        url(r'^logout/$', views.user_logout, name='logout'),
        url(r'^(?P<client_name_slug>[\w\-]+)/$', views.client, name='client'),
)


# Serve media files only in development
if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
   )
    urlpatterns += patterns('', (
        r'^static/(?P<path>.*)$',
        'django.views.static.serve',
        {'document_root': 'static'}
))
else:
    print "no server is configured to serve media files. Do it now."


    </html>

Solution

  • You should remove the entry in your urls.py. All I required to serve static files is the following:

    in my settings.py:

    DEBUG = True
    
    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    )
    
    MIDDLEWARE_CLASSES = (
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    )
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'myprojectname/static'),
    )
    
    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/1.7/howto/static-files/
    
    STATIC_URL = '/static/'
    

    In my template:

    {% load staticfiles %}
    
    <script type="text/javascript" src="{% static 'raphael-min.js' %}"></script>
    

    The file's location is ./myprojectname/static/raphael-min.js IE /home/username/djangoProjects/myprojectname/myprojectname/static/raphael-min.js