Question

I'm just starting with django 1.6 and my css is not working neither my images, I get a 404 error, even the admin(s css is not working

Here is my file structure :

myproject
--myproject
----templates
--static
----css
----media
--myapp
--mp.db
--manage.py

This is my base.html :

<link rel="image_src" href="{% block image_src %}http://{{ request.META.HTTP_HOST }}/static/images/logo.png{% endblock %}" />
<link rel="stylesheet" media="screen" href="{{ STATIC_URL }}css/utils.css">
<link rel="stylesheet" media="screen" href="{{ STATIC_URL }}css/{% block style_link %}style{% endblock %}.css">
   ...
<div id="cont_footer" class="container centerAuto">
<img src="{{ MEDIA_URL }}images/logo_footer.png" alt="" />
</div>

This is my settings :

PROJECT_ROOT = os.path.dirname( __file__ )
PROJECT_NAME = os.path.basename(PROJECT_ROOT)

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static/'),
)

STATICFILES_FINDERS = (
     'django.contrib.staticfiles.finders.FileSystemFinder',
     'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.static',
)
MEDIA_URL = STATIC_URL +'media/'
MEDIA_ROOT = os.path.join(PROJECT_ROOT, *MEDIA_URL.strip("/").split("/"))

In urls.py :

urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name='index.html'), name="index"),
    url(r'^admin/', include(admin.site.urls)),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

What am I doing wrong?I'm pulling my hair off because of this since three days now!!

Thanks

Was it helpful?

Solution

Make folder "static" in root of your project where settings.py exists

In settings.py

STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
                os.path.join(PROJECT_DIR,'static'),
)
TEMPLATE_DIRS = (
                os.path.join(PROJECT_DIR,'template'),
)

In base.html

<link rel="stylesheet" type="text/css" href="{% static 'css/demo.css' %}" /> 

<script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>

In other template files in which you include base.html

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


<script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>


<div id="yourID" class="yourClass">
    <img src="{% static "images/something.gif" %}" alt="something" >
</div>

urls.py

from django.conf.urls import patterns, include, url
from wepl.views import home
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from myApp import settings
from django.views.generic.base import TemplateView
from views import *

urlpatterns = patterns('',
    url(r'^$', 'myApp.views.home', name='home'),

    )

urlpatterns += staticfiles_urlpatterns()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top