1

I'm using Django 1.8. I get status 200 for both of my css files but the firefox says:

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

For whatever reason the files are being served as text/html rather than text/css. This is my html.

  <link href="css/full-width-pics.css" rel="stylesheet" type="text/css" />
    <link href="css/mainstyle.css" rel="stylesheet" type="text/css" />

This is in a base.html file. I extend base.html it in my index.html file. Before I started using template inheritance and had everything in index.html it worked fine.

I'm on Ubuntu. I checked /etc/mime.types. css is listed with text/css.

This has left me really confused

Hauzron
  • 305
  • 1
  • 3
  • 19
  • /etc/mime.types is for the system itself, and it's not necessarily used by applications. You can have a look in Django's settings. Does [this question](http://stackoverflow.com/questions/16303098/django-development-server-and-mime-types) help? – Mr Lister Jul 04 '15 at 13:23
  • I tried that as well. It didn't work for me. Somewhere here on stackoverflow I read Django uses Python's Mime types which depend on the systems mime types. – Hauzron Jul 04 '15 at 17:10
  • Do your setting `DEBUG = False` ? – Edwin Lunando Jul 10 '15 at 08:36
  • What is your url configuration? Have you checked this configuration for development environment: https://docs.djangoproject.com/en/1.8/howto/static-files/#serving-static-files-during-development – kostas trichas Nov 24 '15 at 13:52
  • Can you share your urlconf please? – kostas trichas Nov 24 '15 at 13:53

1 Answers1

0

Year 2020 ANSWER:

if you want any mime content type to auto detect by browser this is the solution.

after many painful failed static attempts this is the dynamic solution.

def fetch(request):
    import mimetypes
    clientRequestUrl=os.getcwd()+'/servlet'+request.path
    try: return HttpResponse(fread(clientRequestUrl), content_type=mimetypes.guess_type(request.path)[0])
    except Exception as e: return HttpResponse(str(e)+'===> Error Thrown <br>')
  • here fread() reads file directly in try except block its just an I/O wrapper
  • in try block --> content_type=mimetypes.guess_type(request.path)[0] does the MIME detection magic [0] ie first element is required because it returns a tuple first being the mime type and second being encoding.
  • request.path is passed in above line because it guesses MIME based on file path. for example text/css for file named stylsheet.css if client(browser) request it.
  • clientRequestUrl = exact url the client is trying to request to server.
nikhil swami
  • 2,360
  • 5
  • 15