Issue
from flask import Flask, render_template
# Flask is a class that allows us to create an app
# render_template is a method offered by flask
app = Flask(__name__) # creates an app with the name of the file
@app.route('/') # route that listens to the homepage
def index(): # route handler
# render_template(template_name_or_list)
# used to specify an html template to render to the user
return render_template('index.html', data=[{
'description': 'Todo 1'
}, {
'description': 'Todo 2'
}, {
'description': 'Todo 3'
}])
# To run the app
# In the terminal
# FLASK_APP=app.py FLASK_DEBUG=true flask run
My application's name is app.py, and it's in a folder named ToDo-App, and within this folder there is another folder called templates, that contains the index.html file.
<html>
<head>
<title>Todo App</title>
</head>
<body>
<ul>
<!-- jinja for loop -->
{% for d in data %}
<li>{{ d.description }}</li>
{% endfor %}
<!--<li>Todo 1</li>
<li>Todo 2</li>
<li>Todo 3</li>
<li>Todo 4</li>-->
</ul>
</body>
</html>
In the terminal:
Esam@DESKTOP-73QDAD3 MINGW32 /i/web/advanced-track/1-sql_and_data_modeling_for_the_web/ToDo-App
$ FLASK_APP=app.py FLASK_DEBUG=true flask run
- Serving Flask app "app.py" (lazy loading)
- Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
- Debug mode: on
- Restarting with stat
- Debugger is active!
- Debugger PIN: 266-552-216
- Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
When I opened this link http://127.0.0.1:5000/
Traceback (most recent call last): File "C:\Users\Esam\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\flask\cli.py", line 236, in locate_app import(module_name) ModuleNotFoundError: No module named 'app'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "C:\Users\Esam\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\flask\cli.py", line 337, in call rv = self._load_unlocked() File "C:\Users\Esam\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\flask\cli.py", line 324, in _load_unlocked self._app = rv = self.loader() File "C:\Users\Esam\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\flask\cli.py", line 381, in load_app app = locate_app(self, import_name, name) File "C:\Users\Esam\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\flask\cli.py", line 246, in locate_app raise NoAppException( flask.cli.NoAppException: Could not import "app".
Solution
Depend on what machine your using, you need to do one of the following:
Unix Bash (Linux, Mac, etc.):
$ export FLASK_APP=hello
$ flask run
Windows CMD:
> set FLASK_APP=hello
> flask run
Windows PowerShell:
> $env:FLASK_APP = "hello"
> flask run
Answered By - AS11
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.