1

New to Flask and have some experience with python, when using render_template it simply does not render the template as well as not giving out any errors. Code Here:

from flask import Flask, render_template

app = Flask(__name__, template_folder= "/templates")




@app.route("/")
def index():
        #return("Index str")
    return render_template("index.html")


@app.route("/crawler")
def crawler():
    return("WebCrawler str")
    return render_template("crawler.html")

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

HTML here (pretty certain file hierarchy is correct).

<!DOCTYPE html>

<html lang="en">
    <head>
        <link rel="stylesheet" type="text/css" href="{{url_for('static', filename='style.css')}}">
        <meta charset = "utf-8">
        <title> Index</title>
    <head>
    <body>
        <p> This webserver hosts all webapps and will showcase any unfinished applications.
            </p>

        </body>

</html>

root virtual environment

static for css

templates folder

3 Answers 3

5

The default folder for the HTML files is templates. So create a folder called "templates" where your python file is located. Put any HTML files in that folder

5
  • The last screenshot shows a templates folder within the same folder that main.py is located.
    – Hobnob
    Jun 17, 2017 at 18:41
  • Have you been trying to access the site through localhost? If so then you need to import os and have this code: if __name__ == '__main__': port = int(os.environ.get('PORT', 5000)) app.run(host='0.0.0.0', port=port) Jun 17, 2017 at 19:04
  • Indeed I have, never thought of that, Ill just test that now.
    – Hobnob
    Jun 17, 2017 at 19:09
  • Thankyou, it was this afterall, are you able to explain why this is the case? Sorry new to web development.
    – Hobnob
    Jun 17, 2017 at 19:12
  • I'm not a huge technical guy so I'm not sure of the exact way to describe it. But basically you need to tell the program to host the site at port 5000 on your local computer. That way when you go to localhost:5000, your site appears there Jun 19, 2017 at 13:08
2

By default Flask template_folder set to templates. If you wish to change it then mention folder in the root path of the application.

It't not necessary when templates folder but it's should be...

app = Flask(__name__, template_folder= "templates")
4
  • I have tried leaving it to default however this didn't work either, I could show screenshots of the filing if you need?
    – Hobnob
    Jun 17, 2017 at 18:28
  • @Hobnob That would be nice. Please update your question with filing screenshot...
    – Raja Simon
    Jun 17, 2017 at 18:28
  • Can you put app.debug = True above the app.run() statement. It will give you better error messages...
    – Raja Simon
    Jun 17, 2017 at 18:47
  • Just sorted it out, to do with being on localhost, cheers anyway.
    – Hobnob
    Jun 17, 2017 at 19:17
0

from flask import Flask, render_template

app = Flask(__name__, template_folder= "templates")
@app.route("/")
def index():
    return render_template('/index.html')

This code might work..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.