Question

I have a simple flask application. I have all my css inside static/css directory. I have created a master template in which I want to include css from that directory. Following is what I have tried so far.

<link rel="stylesheet" href="/static/css/justified-nav.css" type="text/css" media="screen">

<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/justified-nav.css') }}" media="screen">

<link rel="stylesheet" type="text/css" href="http://127.0.0.1/static/css/justified-nav.css" media="screen">

In all the cases the file gets loaded but with mime-type text/plain. I have tried putting the css file directly into the static folder as well but no results.

What am I doing wrong? How can I include a css file in a template?

Was it helpful?

Solution

Flask uses the mimetypes module to determine the MIME type of a file, based on its extension. If you get text/plain for a CSS file it means this module returns the wrong MIME type.

On Windows it uses data from the registry, so if the "Content Type" value in HKCR/.css is not set to the proper MIME type it can cause your problem.

OTHER TIPS

Try this and it should work.

<link rel="stylesheet" href="{{ url_for('static', filename='css/bootstrap.css') }}"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top