简体   繁体   中英

Django development server. CSS files being served as 'text/html'

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM