0

I have downloaded a website template and I want to integrate it with flask. Basically, this template contains asset folder which contains the css and js files. I know that by using render_template('index.html'), I can call index.html on localhost. But when I do so, the webpage is not rendered properly. I think flask is not able to load the js, css, etc files in that folder (templates folder).

My template folder has index.html along with assets folder and othe folders. I am using the code shown below to actually run the index.html.

from flask import Flask, render_template
import os

app = Flask(__name__)

@app.route('/')
def homepage():
    return render_template("index.html")

if __name__ == '__main__':
    app.run(debug=True)

So, index.html is loading but it is not loading the css files, etc. Because if that is not loaded, my webpage looks dull. Its simply some text on white screen. I think the problem is with loading the static files because I get an error saying that there is no folder assets on localhost.

Thanks in advance.

  • This might help you!. https://stackoverflow.com/questions/22259847/application-not-picking-up-css-file-flask-python – bumblebee Feb 06 '19 at 04:24

1 Answers1

1

You need to reference the static files inside your template like this:

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

And ensure you put the stylesheets in the static directory of your project. You can also configure which directory contains static assets with something like:

app = Flask(__name__, static_folder="static_dir")
jcragun
  • 2,060
  • 10
  • 8