0

I am testing my django project locally. The templates and static files are as listed:

static/
    css/
    js/
    ...
templates/
    home.html
    ...

When I run the server, the html file is loaded but firefox indicate that:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

The html links in my templates are like:

<link rel="stylesheet" href="/static/css/bootstrap.min.css" type="text/css"/>

however, when I input 127.0.0.1:8000/static/css/bootstrap.min.css in the browser, the file actually exists and can be fetched.

[26/Apr/2015 06:47:38]"GET / HTTP/1.1" 200 10125
[26/Apr/2015 06:47:49]"GET /static/css/bootstrap.min.css HTTP/1.1" 200 117150

I have go through some postings on this topic, I have done things below: My settings.py have INSTALLED_APPS:

INSTALLED_APPS = (
    'django.contrib.admin',
    ...
    'django.contrib.staticfiles',
    'frontend', 
    'crawler',
}

my urls.py is pasted here:

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = patterns('',
    url(r'^$', include('frontend.urls')),
);

urlpatterns += staticfiles_urlpatterns()

What is the problem here?

8
  • Is your STATIC_URL in settings.py set? Commented Apr 26, 2015 at 6:59
  • 1
    Since you can access the css file directly I assume you have DEBUG = True? staticfiles_urlpatterns() doesn't work if it's False. Commented Apr 26, 2015 at 6:59
  • @codename_subho yes, STATIC_URL = '/static/' Commented Apr 26, 2015 at 7:00
  • @TimSaylor Yes, DEBUG = True Commented Apr 26, 2015 at 7:00
  • 2
    I'm pretty sure this has nothing to do with css. Your css and static files load just fine, as evidenced by your test. The problem is that your web browser interprets the page not as HTML but as XML. I have no idea why this happens, could it be sending the wrong content type for some reason, or do you have a weird doctype? What does your template look like? Commented Apr 26, 2015 at 7:31

1 Answer 1

1

In your urls.py file,

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = patterns('',
    url(r'^$', include('frontend.urls')),
);

urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)

Make sure that your settings.py file has these defined.

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

STATIC_URL = '/static/'

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

STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.FileSystemFinder',
)

Also, in your templates add on the top:

{%load staticfiles%}

These settings work for me.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.