简体   繁体   中英

Flask static css file

I'm working on how to add value from flask into static/css file Here's my code from static/style.css:

.color1 {
    background-color: {{pickcolor}};
    width: 30px;
    height: 30px;
}

.color2 {
    background-color: {{pickcolor}};
    width: 30px;
    height: 30px;
}

so the problem i got is underline error property value expectedcss(css-propertyvalueexpected)

BUT when I use internal css in html file

<style>

    .color1 {
        background-color: {{pickcolor}};
        width: 30px;
        height: 30px;
    }

    .color2 {
        background-color: {{pickcolor}};
        width: 30px;
        height: 30px;
    }


</style>

There is no underline problem with my {{pickcolor}}

Your style.css file is probably not templated . I do not know about your exact project config but static files are usually not templated in general.

If you want to template your CSS file, first move it to the templates folder (usually templates ), you will then have to create a view for it and use the URL of that view instead of a link to a static file. eg

from flask import make_response, render_template

@app.route('/style.css')
def style():
    pickcolor = ...  # whatever

    # we explicitly create the response because we need to edit its headers
    response = make_response(render_template('style.css', pickcolor=pickcolor))

    # required to make the browser know it is CSS
    response['Content-type'] = 'text/css'

    return response

Then, in your HTML template

<html>
  <head>
    <link rel="stylesheet" type="text/css" href="{{ url_for('style') }}">
  </head>
  <!-- ... -->
</html>

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