3

My browser (firefox) console confirms receiving files with status 200 but firefox gives me this:

The stylesheet http://localhost:8000/css/full-width-pics.css was not loaded because its MIME type, "text/html", is not "text/css". localhost:8000
The stylesheet http://localhost:8000/css/mainstyle.css was not loaded because its MIME type, "text/html", is not "text/css". localhost:8000

Just as further poof that Django is serving the files but just with the wrong mimetype: All files give status code 200:

[05/Jul/2015 08:52:33]"GET / HTTP/1.1" 200 6263
[05/Jul/2015 08:52:33]"GET /css/full-width-pics.css HTTP/1.1" 200 6263
[05/Jul/2015 08:52:33]"GET /js/bootstrap.min.js HTTP/1.1" 200 6263
[05/Jul/2015 08:52:33]"GET /css/mainstyle.css HTTP/1.1" 200 6263
[05/Jul/2015 08:52:33]"GET /js/jquery.js HTTP/1.1" 200 6263

This is what I've found out so far.

Django uses python to guess mime types. Python uses the system it runs on to guess mime types. In windows that's somewhere in the registry. I'm on Ubuntu so for me that is in /etc/mime.types.

I found this line in /etc/mime.types:

text/css                    css

That means the problem is not with my system. Is the problem with python then? No. Running the following:

import mimetypes
mimetypes.guess_type("test.css")

gives me the following:

('test/css', none)

I've tried some 'hacks' from other stackoverflow answers. One hack was to add mimetypes.add_type("text/css", ".css", True) to settings.py. It didn't work.

Another was to add url(r'.*\.css$', views.css), to urls.py and this to views.py:

def css(request):
    filename = request.path.strip("/")
    data = open(filename, "rb").read()
    return HttpResponse(data, mimetype="text/css")

Also didn't work. I removed both of them. Now I just want to know why this is happening and how I can properly fix it. I'm sure the problem is with Django but I'm not sure where.

One more important piece of information. This only started happening after I started using template inheritance. Before <link href="css/full-width-pics.css" rel="stylesheet" type="text/css" /> was directly in index.html and it worked fine. Now it is in base.html and index.html 'extends' base.html. I'm learning Django so I just started using template inheritance.

Thanks if you've even read all this.

Hauzron
  • 305
  • 1
  • 3
  • 19
  • 1
    What happens if you try to load http://localhost:8000/css/full-width-pics.css directly in your browser? Does it display the correct content? – Alasdair Jul 05 '15 at 15:50
  • Also having this issue, did you end up fixint it? – orange1 Sep 08 '15 at 16:23
  • @orange1 Unfortunately I didn't. I resorted to using my production server (which uses Gunicorn and NginX) for development instead of django's dev server. It is a pain having to constantly upload files although I'm going to start using Git soon. I could also set up Gunicorn and NginX on my laptop but that's too much extra trouble. Let me know if you find a fix. – Hauzron Sep 08 '15 at 18:32
  • Ok, so I actually did manage to fix it. It turns out that I had a fairly silly error -- I was missing the forward slash for my template links. I'd accidentally created a copy of my template within the directory, though, so I was editing a file that was different from the one that was being served. Not sure if that was your issue, but it might help to double check that your tags have src attributes that look something like `src="/static/js/jquery.js"` ? – orange1 Sep 08 '15 at 22:44

0 Answers0