use a css stylesheet on a jinja2 template

后端 未结 5 713
盖世英雄少女心
盖世英雄少女心 2020-12-28 13:01

I am making a website using html, css, flask and jinja2.

I have a page working on a flask server, the buttons and labels etc. are displayed, but the css stylesheet I

相关标签:
5条回答
  • 2020-12-28 13:42

    Tried almost every solution on Stack Overflow. It only worked for me when I placed the static folder in the same directory as my run.py file. I changed my folder structure from:

    app/
        views
        static
        templates
    run.py
    

    To:

    app/
        views
        templates
    static
    run.py
    

    I guess moving the run.py instead would work too. Have a look at this Jinja Templating Tutorial for extra info. Not sure why I had to change the structure for it to work though.

    0 讨论(0)
  • 2020-12-28 13:46

    To add to what's been said here, be sure to update Jinja2 to v2.10, as lesser versions seem to cause this same bug. Cheers!

    0 讨论(0)
  • 2020-12-28 13:57

    The order of handler might cause the problems:

    url: /stylesheets static_dir: stylesheets
    url: /.* script: helloworld.application
    

    will work instead of

    url: /.* script: helloworld.application
    url: /stylesheets static_dir: stylesheets
    
    0 讨论(0)
  • 2020-12-28 14:00
    <link rel="stylesheet" type="text/css" href="styles.css">
    

    href value must be within quotes.

    make sure the file name and path are proper OR try the below

    <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}"/>
    
    0 讨论(0)
  • 2020-12-28 14:03

    All public files (the ones that are not processed, like templates or python files) should be placed into dedicated static folders. By default, Jinja2 has one static folder called static.

    This should fix your problem:

    1. Move /templates/styles.css to /static/styles.css

    2. Update your code with following code, that will be translated into correct file location:

      <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
      

    More info on static files in Jinja2 is here.

    0 讨论(0)
提交回复
热议问题