I am new to flask and try to run a very simple python file which calls an HTML document, but whenever I search on http://127.0.0.1:5000/, it raises the TemplateNotFound error. I searched stack overflow on similar questions, but even with implementing the solutions, I get the same error. Nothing worked so far
base.html contains:
<body>
<h1>Hello there</h1>
</body>
</html>
flask_test_2.py contains:
from flask import Flask, render_template
app = Flask(__name__, template_folder='templates')
#app.route('/')
def index():
return render_template('base.html')
if __name__ == '__main__':
app.run()
as advised in some of the solutions, I checked the file structure:
/flask_test_2.py
/templates
/base.html
It should work. Since is doesn't, you may take out the
template_folder='templates' portion of you app` assignment and have it this way:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def index():
return render_template('base.html')
if __name__ == '__main__':
app.run()
it will use the templates directory by default.
The issue you are experiencing may be path related. You may also declare the app variable this way:
app = Flask(__name__, template_folder='usr\...\templates')
Related
WHY IT DOES NOT SHOW WHAT I'VE WRITTEN IN HOME.HTML AND IT RESULTS TO THAT ERROR?
from flask import Flask, render_template
app = Flask(__name__)
#app.route("/")
def home_page():
return render_template('home.html')
app.debug = True
if __name__ == "__main__":
app.run()
You do have to specify the port in the app.run() call like app.run(port=8080, debug=True). Also check whether home.html is existing in the templates folder.
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__=="__main__":
app.run()
when I running the above code, I got the error "jinja2.exceptions.TemplateNotFound: index.html" evenif for simple hello world program. if anyone have solution please guide me.
Create a folder called templates which contains all HTML files and include this.
app = Flask(__name__, template_folder="templates", static_folder='static')
This error is caused when you attempt to return, via render_template an html file that cannot be found. They should be stored in a templates folder like this:
yourproject/
app.py
templates/
index.html
However your code makes no reference to either render_template or index.html. Why don't you update your question so that it applies to your error?
If you want to call your templates folder something other than templates then per another answer you could look to use something like this:
app = Flask(__name__, template_folder='MyPreferredTemplateFolderName')
I have a simple app.
app.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return app.send_static_file('../../public/index.html')
if __name__== '__main__':
app.run(debug=True)
When I run it using flask run I get this error:
flask.cli.NoAppException: Could not import app
My FLASK_APP is set to {path-to-app}/app.py, and I am running the command from the folder that the file is in.
Can somebody help?
Your html file "index.html" needs to be in a folder called 'templates' which should be in the folder 'app.py' is also in.
Directory Structure:
__init__:
from flask import flask
app = Flask(__name__)
if __name__ == '__main__'
app.run()
Views:
from app import app
#app.route('/')
def hello_world():
return 'Hello World!'
I hope someone can explain what I am doing wrong here -
I guess I'm not understanding how to properly import app. This results in a 404. However when views is moved back to __init__ everything works properly.
You need to explicitly import your views module in your __init__:
from flask import flask
app = Flask(__name__)
from . import views
Without importing the module, the view registrations are never made.
Do keep the script portion outside of your package. Add a separate file in Final_app (so outside the app directory) that runs your development server; say run.py:
def main():
from app import app
app.run()
if __name__ == '__main__'
main()
I want when i go to localhost:8081 for flask to redirect me to file main.html, how can I do that? Here is the code i started with.
from flask import *
app = Flask(__name__)
#app.route('/')
def start():
return render_template("main.html")
if __name__ == '__main__':
app.run(host='0.0.0.0',port=8081,debug=True)
The page main.html is in the same folder as file that contains this code.
This page in the documentation tells you how to structure your Flask project. Flask will look for templates in the templates folder when you render a template with render_template