hynever / flask_fullstack Goto Github PK
View Code? Open in Web Editor NEWPython全栈开发配套代码
Python全栈开发配套代码
作者你好,
我在抄第五章的时候,使用Flask_SQLAlchemy不能正常运行,没有找到类似的东西,烦请帮忙瞅一眼。
代码如下:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
# MySQL所在的主机名
HOSTNAME = "192.168.99.200"
# MySQL监听的端口号,默认3306
PORT = 3306
# 连接MySQL的用户名,读者用自己设置的
USERNAME = "root"
# 连接MySQL的密码,读者用自己的
PASSWORD = "123456"
# MySQL上创建的数据库名称
DATABASE = "database_learn"
app.config['SQLALCHEMY_DATABASE_URI'] = f"mysql+pymysql://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}/{DATABASE}?charset=utf8mb4"
db = SQLAlchemy(app)
with db.engine.connect() as conn:
rs = conn.execute("select 1")
print(rs.fetchone())
报错如下:
FLASK_APP = app.py
FLASK_ENV = development
FLASK_DEBUG = 1
In folder D:/personal/code/database
D:\personal\code\database\venv\Scripts\python.exe -m flask run --port=8000 --host=0.0.0.0
Traceback (most recent call last):
File "C:\Users\52312\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\52312\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 87, in run_code
exec(code, run_globals)
File "D:\personal\code\database\venv\lib\site-packages\flask_main.py", line 3, in
main()
File "D:\personal\code\database\venv\lib\site-packages\flask\cli.py", line 1047, in main
cli.main()
File "D:\personal\code\database\venv\lib\site-packages\click\core.py", line 1055, in main
rv = self.invoke(ctx)
File "D:\personal\code\database\venv\lib\site-packages\click\core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "D:\personal\code\database\venv\lib\site-packages\click\core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "D:\personal\code\database\venv\lib\site-packages\click\core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "D:\personal\code\database\venv\lib\site-packages\click\decorators.py", line 84, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "D:\personal\code\database\venv\lib\site-packages\click\core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "D:\personal\code\database\venv\lib\site-packages\flask\cli.py", line 911, in run_command
raise e from None
File "D:\personal\code\database\venv\lib\site-packages\flask\cli.py", line 897, in run_command
app = info.load_app()
File "D:\personal\code\database\venv\lib\site-packages\flask\cli.py", line 308, in load_app
app = locate_app(import_name, name)
File "D:\personal\code\database\venv\lib\site-packages\flask\cli.py", line 218, in locate_app
import(module_name)
File "D:\personal\code\database\app.py", line 22, in
with db.engine.connect() as conn:
File "D:\personal\code\database\venv\lib\site-packages\flask_sqlalchemy\extension.py", line 642, in engine
return self.engines[None]
File "D:\personal\code\database\venv\lib\site-packages\flask_sqlalchemy\extension.py", line 628, in engines
app = current_app._get_current_object() # type: ignore[attr-defined]
File "D:\personal\code\database\venv\lib\site-packages\werkzeug\local.py", line 513, in _get_current_object
raise RuntimeError(unbound_message) from None
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.
进程已结束,退出代码1
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.