Application not picking up .css file (flask/python)

前端 未结 10 1801
悲&欢浪女
悲&欢浪女 2020-11-28 03:54

I am rendering a template, that I am attempting to style with an external style sheet. File structure is as follows.

/app
    - app_runner.py
    /services
          


        
相关标签:
10条回答
  • 2020-11-28 04:11

    You need to have a 'static' folder setup (for css/js files) unless you specifically override it during Flask initialization. I am assuming you did not override it.

    Your directory structure for css should be like:

    /app
        - app_runner.py
        /services
            - app.py 
        /templates
            - mainpage.html
        /static
            /styles
                - mainpage.css
    

    Notice that your /styles directory should be under /static

    Then, do this

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

    Flask will now look for the css file under static/styles/mainpage.css

    0 讨论(0)
  • 2020-11-28 04:12

    I'm running version 1.0.2 of flask right now. The above file structures did not work for me, but I found one that did, which are as follows:

         app_folder/ flask_app.py/ static/ style.css/ templates/
         index.html
    

    (Please note that 'static' and 'templates' are folders, which should be named exactly the same thing.)

    To check what version of flask you are running, you should open Python in terminal and type the following accordingly:

    import flask

    flask --version

    0 讨论(0)
  • 2020-11-28 04:13

    In jinja2 templates (which flask uses), use

    href="{{ url_for('static', filename='mainpage.css')}}"
    

    The static files are usually in the static folder, though, unless configured otherwise.

    0 讨论(0)
  • 2020-11-28 04:14

    If any of the above method is not working and you code is perfect then try hard refreshing by pressing Ctrl + F5. It will clear all the chaces and then reload file. It worked for me.

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