The flask object implements a WSGI application and acts as the central object. It is passed the name of the module or package of the application. Once it is created it will act as a central registry for the view functions, the URL rules, template configuration and much more.
……
The idea of the first parameter is to give Flask an idea of what belongs to your application. This name is used to find resources on the filesystem, can be used by extensions to improve debugging information and a lot more.
So it’s important what you provide there. If you are using a single module, __name__ is always the correct value. If you however are using a package, it’s usually recommended to hardcode the name of your package there.
……
Why is that? The application will work even with __name__, thanks to how resources are looked up. However it will make debugging more painful. Certain extensions can make assumptions based on the import name of your application. For example the Flask-SQLAlchemy extension will look for the code in your application that triggered an SQL query in debug mode. If the import name is not properly set up, that debugging information is lost. (For example it would only pick up SQL queries in yourapplication.app and not yourapplication.views.frontend)
@app.route('/') @app.route('/index') defindex(): user = {"username": "zyen", "body": "want to be a best hacker!"} return render_template("index.html", user=user)
(venv) C:\Users\zyen\PycharmProjects\myblog>python app.py C:\Users\zyen\PycharmProjects\myblog\venv\lib\site-packages\flask_sqlalchemy\__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will b e disabled by default in the future. Set it to TrueorFalse to suppress this warning. warnings.warn(FSADeprecationWarning( usage: app.py [-?] {db,shell,runserver} ...
positional arguments: {db,shell,runserver} db Perform database migrations shell Runs a Python shell inside Flask application context. runserver Runs the Flask development server i.e. app.run()
optional arguments: -?, --help show this help message and exit
(venv) C:\Users\zyen\PycharmProjects\myblog>python app.py db C:\Users\zyen\PycharmProjects\myblog\venv\lib\site-packages\flask_sqlalchemy\__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will b e disabled by default in the future. Set it to TrueorFalse to suppress this warning. warnings.warn(FSADeprecationWarning( usage: Perform database migrations
Perform database migrations
positional arguments: {init,revision,migrate,edit,merge,upgrade,downgrade,show,history,heads,branches,current,stamp} init Creates a new migration repository revision Create a new revision file. migrate Alias for'revision --autogenerate' edit Edit current revision. merge Merge two revisions together. Creates a new migration file upgrade Upgrade to a later version downgrade Revert to a previous version show Show the revision denoted by the given symbol. history List changeset scripts in chronological order. heads Show current available heads in the script directory branches Show current branch points current Display the current revision for each database. stamp 'stamp' the revision table with the given revision; don't run any migrations optional arguments: -?, --help show this help message and exit
(venv) C:\Users\zyen\PycharmProjects\myblog>python app.py db init C:\Users\zyen\PycharmProjects\myblog\venv\lib\site-packages\flask_sqlalchemy\__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will b e disabled by default in the future. Set it to True or False to suppress this warning. warnings.warn(FSADeprecationWarning( Creating directory C:\Users\zyen\PycharmProjects\myblog\migrations ... done Creating directory C:\Users\zyen\PycharmProjects\myblog\migrations\versions ... done Generating C:\Users\zyen\PycharmProjects\myblog\migrations\alembic.ini ... done Generating C:\Users\zyen\PycharmProjects\myblog\migrations\env.py ... done Generating C:\Users\zyen\PycharmProjects\myblog\migrations\README ... done Generating C:\Users\zyen\PycharmProjects\myblog\migrations\script.py.mako ... done Please edit configuration/connection/logging settings in 'C:\\Users\\zyen\\PycharmProjects\\myblog\\migrations\\alembic.ini' before proceeding.
(venv) C:\Users\zyen\PycharmProjects\myblog>python app.py db migrate C:\Users\zyen\PycharmProjects\myblog\venv\lib\site-packages\flask_sqlalchemy\__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will b e disabled by default in the future. Set it to True or False to suppress this warning. warnings.warn(FSADeprecationWarning( INFO [alembic.runtime.migration] Context impl MySQLImpl. INFO [alembic.runtime.migration] Will assume non-transactional DDL. INFO [alembic.autogenerate.compare] Detected added table 'user' INFO [alembic.autogenerate.compare] Detected added index 'ix_user_email' on '['email']' INFO [alembic.autogenerate.compare] Detected added index 'ix_user_username' on '['username']' INFO [alembic.autogenerate.compare] Detected added table 'post' INFO [alembic.autogenerate.compare] Detected added index 'ix_post_timeStep' on '['timeStep']' Generating C:\Users\zyen\PycharmProjects\myblog\migrations\versions\d0b26b497fd8_.py ... done
接下来通过upgrade来更新表
1 2 3 4 5 6 7
(venv) C:\Users\zyen\PycharmProjects\myblog>python app.py db upgrade C:\Users\zyen\PycharmProjects\myblog\venv\lib\site-packages\flask_sqlalchemy\__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will b e disabled by default in the future. Set it to True or False to suppress this warning. warnings.warn(FSADeprecationWarning( INFO [alembic.runtime.migration] Context impl MySQLImpl. INFO [alembic.runtime.migration] Will assume non-transactional DDL. INFO [alembic.runtime.migration] Running upgrade -> d0b26b497fd8, empty message
<!-- Collect the nav links, forms, and other content for toggling --> <divclass="collapse navbar-collapse"id="bs-example-navbar-collapse-1"> <ulclass="nav navbar-nav"> <liclass="active"><ahref="#"></span>Home </a></li> <li><ahref="#"></a>About</a></li> <li><ahref="#">Categories</a></li> </ul>